From b7011cbda9050749f2426bbf5e79fd345fbc6e35 Mon Sep 17 00:00:00 2001 From: rastringer Date: Wed, 4 Nov 2015 11:42:52 -0800 Subject: [PATCH 1/3] Adds GET, POST --- .gitignore | 2 ++ gulpfile.js | 43 ++++++++++++++++++++++++++++++++ lib/index.html | 20 +++++++++++++++ lib/router.js | 24 ++++++++++++++++++ lib/server.js | 11 +++++++++ package.json | 25 +++++++++++++++++++ test/test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 191 insertions(+) create mode 100644 .gitignore create mode 100644 gulpfile.js create mode 100644 lib/index.html create mode 100644 lib/router.js create mode 100644 lib/server.js create mode 100644 package.json create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91dfed8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..d80e091 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,43 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['server.js', './lib/**/*.js', 'bin/**/*.js']; +var testFiles = ['test/**/*.js']; + +gulp.task('mocha:test', function() { + return gulp.src(testFiles, appFiles) + .pipe(mocha({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(mocha({reporter: 'nyan'})); +}); + +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('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint']); diff --git a/lib/index.html b/lib/index.html new file mode 100644 index 0000000..069c2b4 --- /dev/null +++ b/lib/index.html @@ -0,0 +1,20 @@ + + + + + + Routes implemented by API + + + +

Vanilla HTTP Server

+ + + + diff --git a/lib/router.js b/lib/router.js new file mode 100644 index 0000000..73e0e5a --- /dev/null +++ b/lib/router.js @@ -0,0 +1,24 @@ + +'use strict' + +var fs = require('fs'); +var url = require('url'); + +// Respond to a request to /time that will send back the current time of the server. + +if(request.url === '/time' && request.method.toLowerCase() === 'get') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end(new Date().toString()); +} + +// Respond to a get request to /greet/name where name is any single word string. + +if(request.url === '/greet/name' && request.method.toLowerCase() === 'get') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end('Hello ' + name + '!'); +}); + +if(request.url === '/greet' && request.method.toLowerCase() === 'post') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end('Hello ' + name + '!'); +} diff --git a/lib/server.js b/lib/server.js new file mode 100644 index 0000000..c7aef92 --- /dev/null +++ b/lib/server.js @@ -0,0 +1,11 @@ + +// Create web server +var router = require(__dirname + '/lib/route'); + +var http = require('http'); +http.createServer(function(request, response) { + homeRoute(request, response); +}).listen(3000); +console.log('Server up and running'); + +var server = http.createServer(function(request, res) diff --git a/package.json b/package.json new file mode 100644 index 0000000..d6eedcf --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "robin_stringer", + "version": "0.0.1", + "description": "Vanilla HTTP Server", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rastringer/create_an_http_server.git" + }, + "author": "ra1stringer@gmail.com", + "license": "MIT", + "bugs": { + "url": "https://github.com/rastringer/create_an_http_server/issues" + }, + "homepage": "https://github.com/rastringer/create_an_http_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "jshint": "^2.8.0" + } +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..4ea3807 --- /dev/null +++ b/test/test.js @@ -0,0 +1,66 @@ + +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../server'); + +describe('GET', function() { + before(function() { + this.indexFile = fs.readFileSync(__dirname + '/../public/index.html').toString(); + }); + + it('should be able to get an index', function(done) { + debugger; + chai.request('localhost:3000') + .get('/') + .end(function(err, response) { + debugger; + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql(this.indexFileString); + done(); + }.bind(this)); + }); +}); + +describe('GET /time', function() { + it('should send back time: (PST)', function(done) { + chai.request('localhost:3000') + .get('/time') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text.split(' ').pop()).to.eql('(PST)'); + done(); + }); + }); +}); + +describe('GET greet/:name', function() { + it('should greet by name', function(done) { + chai.request('localhost:3000') + .get('/greet/test') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql('Hello!'); + done(); + }); + }); +}); + +describe('POST greet/', function() { + it('should greet your name', function(done) { + chai.request('localhost:3000') + .post('/greet') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql('Hello test!'); + done(); + }); + }); +}); + From 7960a7cba2f1f47f850c5f7e705a0bdd79036fe6 Mon Sep 17 00:00:00 2001 From: rastringer Date: Mon, 9 Nov 2015 15:59:57 -0800 Subject: [PATCH 2/3] wraps into folder --- .DS_Store | Bin 0 -> 6148 bytes robin_stringer | 1 + 2 files changed, 1 insertion(+) create mode 100644 .DS_Store create mode 160000 robin_stringer diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..491ff1815b68ab1c4e7921f12043f85a891438ca GIT binary patch literal 6148 zcmeHKUu)Yi5I?2qX2(!Gwn1SpdL85;r5)|wysUd11Uy>GS~44(Mqs31I}IfS{Jc*w z_Wyf~?K|wF?5%f_Qd-jOaScTWr{C#xC)vL!l#Ynq94*gj2|&OsCa2EmVqef6{Rws=C+v&01^i z^?Ua{zY_#OCkP*OPU4Z8#&uFps(wZAGOLbbGvA@e>d`QKJSg)sOdTXLKM<8jMIq{Q&-Z=LABLN=+4l48 zF5lYQn|JwacemH&J3GDkJpAB!Po6$|@%m^yfB*5*=PzHsegE;(REFV4tmUD_1^hze zpCp~dr7V;@#kj>Bi(H3}0mp!2U=lEVbo6r@7g=l!nsi`#Wt_*Z%DAHC zbvUrdplclij)Ch8teI{|=l`p}-~X>Cxt?RdF>t3CVD0^QzmKnE&(@`vqqCNyzC$IU pxW?dR3L5$-##lOvH&L~qpOb+YS!@iV2gUseXc}DO82GOYTmbL8YR3Qo literal 0 HcmV?d00001 diff --git a/robin_stringer b/robin_stringer new file mode 160000 index 0000000..b7011cb --- /dev/null +++ b/robin_stringer @@ -0,0 +1 @@ +Subproject commit b7011cbda9050749f2426bbf5e79fd345fbc6e35 From 45a0630864098edc32d98f0f38c4741fbe5605b2 Mon Sep 17 00:00:00 2001 From: rastringer Date: Mon, 9 Nov 2015 16:11:32 -0800 Subject: [PATCH 3/3] wraps into folder --- README.md | 27 --------------------- gulpfile.js | 43 -------------------------------- lib/index.html | 20 --------------- lib/router.js | 24 ------------------ lib/server.js | 11 --------- package.json | 25 ------------------- test/test.js | 66 -------------------------------------------------- 7 files changed, 216 deletions(-) delete mode 100644 README.md delete mode 100644 gulpfile.js delete mode 100644 lib/index.html delete mode 100644 lib/router.js delete mode 100644 lib/server.js delete mode 100644 package.json delete mode 100644 test/test.js diff --git a/README.md b/README.md deleted file mode 100644 index 82f1087..0000000 --- a/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Vanilla HTTP Server -=========================== -To complete this assignment: - * fork this repository (the sub module for this specific assignment) - * clone down your fork - * place all of your work in a folder that is your full name, use `_`s instead of spaces - * push back up to your fork - * create a pull request back to the original repo - * submit a link to the PR in canvas - -Assingment Description ---------------------------- -For this assignment you should write an http server in vanilla node that responds to several different routes. - -The server should respond to a request to /time that will send back the current time of the server. - - * It should also respond to a get request to /greet/name where name is any single word string. - * It should send back a string that greets that name. - * It should also have a separate post request to /greet that takes the name in JSON format. - * There should be tests using chaiHTTP for both routes, as well as a gulpfile/package.json - * You should have an html page that describes the routes implemented by the api available at the root of the server - - -Rubric: - * Tests: 4pts - * Routes: 4pts - * Organization and gulpfile/package.json 2pts diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index d80e091..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,43 +0,0 @@ -var gulp = require('gulp'); -var jshint = require('gulp-jshint'); -var mocha = require('gulp-mocha'); -var appFiles = ['server.js', './lib/**/*.js', 'bin/**/*.js']; -var testFiles = ['test/**/*.js']; - -gulp.task('mocha:test', function() { - return gulp.src(testFiles, appFiles) - .pipe(mocha({ - node: true, - globals: { - describe: true, - it: true, - before: true, - after: true - } - })) - .pipe(mocha({reporter: 'nyan'})); -}); - -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('jshint', ['jshint:test', 'jshint:app']); -gulp.task('default', ['jshint']); diff --git a/lib/index.html b/lib/index.html deleted file mode 100644 index 069c2b4..0000000 --- a/lib/index.html +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - Routes implemented by API - - - -

Vanilla HTTP Server

- - - - diff --git a/lib/router.js b/lib/router.js deleted file mode 100644 index 73e0e5a..0000000 --- a/lib/router.js +++ /dev/null @@ -1,24 +0,0 @@ - -'use strict' - -var fs = require('fs'); -var url = require('url'); - -// Respond to a request to /time that will send back the current time of the server. - -if(request.url === '/time' && request.method.toLowerCase() === 'get') { - response.writeHead(200, {'Content-Type': 'text/plain'}); - response.end(new Date().toString()); -} - -// Respond to a get request to /greet/name where name is any single word string. - -if(request.url === '/greet/name' && request.method.toLowerCase() === 'get') { - response.writeHead(200, {'Content-Type': 'text/plain'}); - response.end('Hello ' + name + '!'); -}); - -if(request.url === '/greet' && request.method.toLowerCase() === 'post') { - response.writeHead(200, {'Content-Type': 'text/plain'}); - response.end('Hello ' + name + '!'); -} diff --git a/lib/server.js b/lib/server.js deleted file mode 100644 index c7aef92..0000000 --- a/lib/server.js +++ /dev/null @@ -1,11 +0,0 @@ - -// Create web server -var router = require(__dirname + '/lib/route'); - -var http = require('http'); -http.createServer(function(request, response) { - homeRoute(request, response); -}).listen(3000); -console.log('Server up and running'); - -var server = http.createServer(function(request, res) diff --git a/package.json b/package.json deleted file mode 100644 index d6eedcf..0000000 --- a/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "robin_stringer", - "version": "0.0.1", - "description": "Vanilla HTTP Server", - "main": "server.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/rastringer/create_an_http_server.git" - }, - "author": "ra1stringer@gmail.com", - "license": "MIT", - "bugs": { - "url": "https://github.com/rastringer/create_an_http_server/issues" - }, - "homepage": "https://github.com/rastringer/create_an_http_server#readme", - "devDependencies": { - "chai": "^3.4.0", - "chai-http": "^1.0.0", - "gulp": "^3.9.0", - "jshint": "^2.8.0" - } -} diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 4ea3807..0000000 --- a/test/test.js +++ /dev/null @@ -1,66 +0,0 @@ - -var chai = require('chai'); -var chaihttp = require('chai-http'); -chai.use(chaihttp); -var expect = chai.expect; -var fs = require('fs'); -require(__dirname + '/../server'); - -describe('GET', function() { - before(function() { - this.indexFile = fs.readFileSync(__dirname + '/../public/index.html').toString(); - }); - - it('should be able to get an index', function(done) { - debugger; - chai.request('localhost:3000') - .get('/') - .end(function(err, response) { - debugger; - expect(err).to.eql(null); - expect(response).to.have.status(200); - expect(response.text).to.eql(this.indexFileString); - done(); - }.bind(this)); - }); -}); - -describe('GET /time', function() { - it('should send back time: (PST)', function(done) { - chai.request('localhost:3000') - .get('/time') - .end(function(err, response) { - expect(err).to.eql(null); - expect(response).to.have.status(200); - expect(response.text.split(' ').pop()).to.eql('(PST)'); - done(); - }); - }); -}); - -describe('GET greet/:name', function() { - it('should greet by name', function(done) { - chai.request('localhost:3000') - .get('/greet/test') - .end(function(err, response) { - expect(err).to.eql(null); - expect(response).to.have.status(200); - expect(response.text).to.eql('Hello!'); - done(); - }); - }); -}); - -describe('POST greet/', function() { - it('should greet your name', function(done) { - chai.request('localhost:3000') - .post('/greet') - .end(function(err, response) { - expect(err).to.eql(null); - expect(response).to.have.status(200); - expect(response.text).to.eql('Hello test!'); - done(); - }); - }); -}); -