diff --git a/isabella_organ/.gitignore b/isabella_organ/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/isabella_organ/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/isabella_organ/gulpfile.js b/isabella_organ/gulpfile.js new file mode 100644 index 0000000..1a7b23b --- /dev/null +++ b/isabella_organ/gulpfile.js @@ -0,0 +1,37 @@ +'use strict'; + +var gulp = require('gulp'); +var mocha = require('gulp-mocha'); +var jshint = require('gulp-jshint'); +var appFiles = ['http_server.js']; +var testFiles = ['./test/**/*.js']; + +gulp.task('jshint:test', function() { + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('jshint:app', function() { + return gulp.src(appFiles) + .pipe(jshint({ + node: true + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('mocha', function() { + return gulp.src('test/*.js') + .pipe(mocha({ reporter: 'nyan' })); +}); + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint', 'mocha']); diff --git a/isabella_organ/http_server.js b/isabella_organ/http_server.js new file mode 100644 index 0000000..d7fa369 --- /dev/null +++ b/isabella_organ/http_server.js @@ -0,0 +1,42 @@ +var http = require('http'); +var fs = require('fs'); +var url = require('url'); +var ReadStream = require('stream').Readable; + +var server = http.createServer(function(req, res, err) { + var resData = {}; + if (req.url === './' && req.method === 'GET') { + fs.readfile('./public/index.html', function(err, data) { + if (err) return console.log(err); + }); + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + res.write('./public/index.html').toString(); + return res.end(); + } else { + if (req.url === '/time' && req.method === 'GET') { + if (err) return console.log(err); + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + resData.data = 'Time is ' + new Date().toString(); + } + if (req.url === '/greet' && req.method === 'GET') { + if (err) return console.log(err); + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + resData.data = 'hello'; + } + + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain' + }); + res.write(resData.data || 'not found'); + res.end(); + } +}); +server.listen(3000, function() { + console.log('server up'); +}); diff --git a/isabella_organ/package.json b/isabella_organ/package.json new file mode 100644 index 0000000..b6d98f5 --- /dev/null +++ b/isabella_organ/package.json @@ -0,0 +1,36 @@ +{ + "name": "isabella_organ", + "version": "1.0.0", + "description": "HTTP server", + "main": "http_server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/isabellaorgan/create_an_http_server.git" + }, + "keywords": [ + "http", + "server", + "chai", + "chai-http", + "mocha" + ], + "author": "isabellaorgan@gmail.com", + "license": "ISC", + "bugs": { + "url": "https://github.com/isabellaorgan/create_an_http_server/issues" + }, + "homepage": "https://github.com/isabellaorgan/create_an_http_server#readme" +} diff --git a/isabella_organ/public/css/reset.css b/isabella_organ/public/css/reset.css new file mode 100644 index 0000000..af94440 --- /dev/null +++ b/isabella_organ/public/css/reset.css @@ -0,0 +1,48 @@ +/* http://meyerweb.com/eric/tools/css/reset/ + v2.0 | 20110126 + License: none (public domain) +*/ + +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td, +article, aside, canvas, details, embed, +figure, figcaption, footer, header, hgroup, +menu, nav, output, ruby, section, summary, +time, mark, audio, video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} +/* HTML5 display-role reset for older browsers */ +article, aside, details, figcaption, figure, +footer, header, hgroup, menu, nav, section { + display: block; +} +body { + line-height: 1; +} +ol, ul { + list-style: none; +} +blockquote, q { + quotes: none; +} +blockquote:before, blockquote:after, +q:before, q:after { + content: ''; + content: none; +} +table { + border-collapse: collapse; + border-spacing: 0; +} \ No newline at end of file diff --git a/isabella_organ/public/css/styles.css b/isabella_organ/public/css/styles.css new file mode 100644 index 0000000..c53f5c5 --- /dev/null +++ b/isabella_organ/public/css/styles.css @@ -0,0 +1,48 @@ +/* Table of Contents +Body +Text +*/ + +body { + text-align: center; + width: 90%; + margin: 0 auto; + background-color: #00bfff; + color: #000000; + font-family: Arial, Helvetica, sans-serif; + font-size: 16px; +} +a:link { + color: #14c7de; + text-decoration: none;} +a:visited { + color: #01B0f0;} +a:hover { + color: #AEEE00; + text-decoration: underline; +} + +h1 { + text-align: center; + letter-spacing: 15px; + font-size: 56px; + color: #000000; +} + +p { + height: 75%; + width: 75%; + margin-left: 20px; + padding-left: 20px; + text-align: left; + } + + + + + + + + + + diff --git a/isabella_organ/public/index.html b/isabella_organ/public/index.html new file mode 100644 index 0000000..2d250b8 --- /dev/null +++ b/isabella_organ/public/index.html @@ -0,0 +1,15 @@ + + + +
+ +It should return some stuff.
+ \ No newline at end of file diff --git a/isabella_organ/server.js b/isabella_organ/server.js new file mode 100644 index 0000000..8f9dfd0 --- /dev/null +++ b/isabella_organ/server.js @@ -0,0 +1,46 @@ +var http = require('http'); +var fs = require('fs'); +var url = require('url'); +var ReadStream = require('stream').Readable; + +var server = http.createServer(function(req, res, err) { + var resData = {}; + if (req.url === './' && req.method === 'GET') { + fs.readfile('./public/index.html', function(err, data) { + if (err) return console.log(err); + }); + res.writeHead(200, { + 'Content-Type': 'text/html' + }); + res.write('./public/index.html').toString(); + return res.end(); + } else { + if (req.url === '/time' && req.method === 'GET') { + if (err) return console.log(err); + + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + resData.data = 'Time is ' + new Date().toString(); + } + + if (req.url === '/greet' && req.method === 'GET') { + if (err) return console.log(err); + + res.writeHead(200, { + 'Content-Type': 'text/plain' + }); + resData.data = 'hello'; + } + + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain' + }); + res.write(resData.data || 'not found'); + res.end(); + } +}); + +server.listen(3000, function() { + console.log('server up'); +}); diff --git a/isabella_organ/test/test.js b/isabella_organ/test/test.js new file mode 100644 index 0000000..22274d2 --- /dev/null +++ b/isabella_organ/test/test.js @@ -0,0 +1,23 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '../http_server.js'); + +describe('our server', function() { + before(function() { + this.indexFileString = fs.readFileSync(__dirname + '/../public/index.html').toString(); + }); + + it('should be able to get an index', function(done) { + chai.request('localhost:3000') + .get('/') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(this.indexFileString); + done(); + }.bind(this)); + }); +});