From b22d81837581d71d7570776955a6b539558f17c5 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 22:38:32 -0800 Subject: [PATCH 1/8] init basic structure --- README.md | 27 --------------------------- matthew_ringel/.gitignore | 1 + matthew_ringel/index.js | 0 matthew_ringel/lib/server.js | 0 matthew_ringel/package.json | 32 ++++++++++++++++++++++++++++++++ matthew_ringel/public/index.html | 10 ++++++++++ 6 files changed, 43 insertions(+), 27 deletions(-) delete mode 100644 README.md create mode 100644 matthew_ringel/.gitignore create mode 100644 matthew_ringel/index.js create mode 100644 matthew_ringel/lib/server.js create mode 100644 matthew_ringel/package.json create mode 100644 matthew_ringel/public/index.html 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/matthew_ringel/.gitignore b/matthew_ringel/.gitignore new file mode 100644 index 0000000..2ed8d17 --- /dev/null +++ b/matthew_ringel/.gitignore @@ -0,0 +1 @@ +/node_modules/** diff --git a/matthew_ringel/index.js b/matthew_ringel/index.js new file mode 100644 index 0000000..e69de29 diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js new file mode 100644 index 0000000..e69de29 diff --git a/matthew_ringel/package.json b/matthew_ringel/package.json new file mode 100644 index 0000000..5fc73a2 --- /dev/null +++ b/matthew_ringel/package.json @@ -0,0 +1,32 @@ +{ + "name": "vanillahttp", + "version": "0.1.0", + "description": "vanilla http server for week 2 of sea-d45-javascript", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mringel/create_an_http_server.git" + }, + "keywords": [ + "http", + "node" + ], + "author": "matthew ringel", + "license": "MIT", + "bugs": { + "url": "https://github.com/mringel/create_an_http_server/issues" + }, + "homepage": "https://github.com/mringel/create_an_http_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "mocha": "^2.3.3" + } +} diff --git a/matthew_ringel/public/index.html b/matthew_ringel/public/index.html new file mode 100644 index 0000000..442d97f --- /dev/null +++ b/matthew_ringel/public/index.html @@ -0,0 +1,10 @@ + + + + + Document + + + + + From d669119d21f22089a04f7f1c6eaee22d2b86aec2 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 23:14:09 -0800 Subject: [PATCH 2/8] basic request handlers done --- matthew_ringel/lib/requesthandler.js | 4 ++++ matthew_ringel/lib/server.js | 1 + matthew_ringel/public/index.html | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 matthew_ringel/lib/requesthandler.js diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js new file mode 100644 index 0000000..d1cf268 --- /dev/null +++ b/matthew_ringel/lib/requesthandler.js @@ -0,0 +1,4 @@ + +function handleRequest(request, response) { + +} diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js index e69de29..ebc131e 100644 --- a/matthew_ringel/lib/server.js +++ b/matthew_ringel/lib/server.js @@ -0,0 +1 @@ +var http = require('http'); diff --git a/matthew_ringel/public/index.html b/matthew_ringel/public/index.html index 442d97f..5f226d5 100644 --- a/matthew_ringel/public/index.html +++ b/matthew_ringel/public/index.html @@ -2,9 +2,9 @@ - Document + Vanilla HTTP Server of Awesome - +

Routes

From 0a3488463d3e75830797b9d3b9fed9c3ddd282c4 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 23:15:31 -0800 Subject: [PATCH 3/8] minor edit --- matthew_ringel/lib/requesthandler.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js index d1cf268..f9dba1d 100644 --- a/matthew_ringel/lib/requesthandler.js +++ b/matthew_ringel/lib/requesthandler.js @@ -1,4 +1,16 @@ -function handleRequest(request, response) { - +function greet(request, response) { + var name = url.parse(request.url).pathname.split('/').pop(); + var body = "Hello " + name.toString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(body); + response.end(); +} + +function time(request, response) { + var date = new Date(); + var time = date.toUTCString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(time); + response.end(); } From 3099aa4908a9d0f7be1a443d9fcd07766ede1a74 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Tue, 3 Nov 2015 23:34:31 -0800 Subject: [PATCH 4/8] time response done --- matthew_ringel/lib/requesthandler.js | 9 +++++++++ matthew_ringel/lib/server.js | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js index f9dba1d..537c9c8 100644 --- a/matthew_ringel/lib/requesthandler.js +++ b/matthew_ringel/lib/requesthandler.js @@ -14,3 +14,12 @@ function time(request, response) { response.write(time); response.end(); } + +// var test = function(string) { +// console.log('logging from ' + string); +// }; +// +// test("requesthandler"); + +exports.greet = greet; +exports.time = time; diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js index ebc131e..d6e7538 100644 --- a/matthew_ringel/lib/server.js +++ b/matthew_ringel/lib/server.js @@ -1 +1,13 @@ var http = require('http'); +var requestHandler = require('./requesthandler'); +// test("server.js"); + +var server = http.createServer(function(request, response) { + if (request.url === '/time') { + requestHandler.time(request, response); + } +}); + +server.listen(3000, function() { + console.log('server running on port 3000'); +}); From 84cc053524a5e9f87bec98fb63eea924f9657f12 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Wed, 4 Nov 2015 11:04:48 -0800 Subject: [PATCH 5/8] post request to /greet with JSON responds with plain text --- matthew_ringel/README.md | 16 ++++++++++++++++ matthew_ringel/index.js | 2 ++ matthew_ringel/lib/requesthandler.js | 27 ++++++++++++++++++++++----- matthew_ringel/lib/server.js | 11 ++++++++++- matthew_ringel/test/server_test.js | 13 +++++++++++++ 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 matthew_ringel/README.md create mode 100644 matthew_ringel/test/server_test.js diff --git a/matthew_ringel/README.md b/matthew_ringel/README.md new file mode 100644 index 0000000..0d9c643 --- /dev/null +++ b/matthew_ringel/README.md @@ -0,0 +1,16 @@ +#Vanilla Http Server +**Matthew Ringel** +**Code Fellows sea-d45-javascript** +**4 November 2015** + + + + + + + + + + + +http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js diff --git a/matthew_ringel/index.js b/matthew_ringel/index.js index e69de29..affc685 100644 --- a/matthew_ringel/index.js +++ b/matthew_ringel/index.js @@ -0,0 +1,2 @@ +require(__dirname + '/lib/requesthandler'); +require(__dirname + '/lib/server.js'); diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js index 537c9c8..a925ce6 100644 --- a/matthew_ringel/lib/requesthandler.js +++ b/matthew_ringel/lib/requesthandler.js @@ -1,3 +1,5 @@ +var url = require('url'); +var qs = require('querystring'); function greet(request, response) { var name = url.parse(request.url).pathname.split('/').pop(); @@ -15,11 +17,26 @@ function time(request, response) { response.end(); } -// var test = function(string) { -// console.log('logging from ' + string); -// }; -// -// test("requesthandler"); +function greetJSON(request, response) { + console.log('handling POST greet with JSON'); + var postData = ''; + request.on('data', function (data) { + postData += data; + }); + request.on('end', function() { + var postName = JSON.parse(postData); + console.log('Received JSON: '); + console.dir(postName); + console.log(postName.name); + + var name = postName.name; + var body = "hello " + name.toString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(body); + response.end(); + }); +} exports.greet = greet; exports.time = time; +exports.greetJSON = greetJSON; diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js index d6e7538..4ba8624 100644 --- a/matthew_ringel/lib/server.js +++ b/matthew_ringel/lib/server.js @@ -1,11 +1,20 @@ var http = require('http'); +var url = require('url'); + var requestHandler = require('./requesthandler'); -// test("server.js"); var server = http.createServer(function(request, response) { if (request.url === '/time') { requestHandler.time(request, response); } + if (url.parse(request.url).pathname.split('/').length === 3 && + url.parse(request.url).pathname.split('/')[1] === 'greet' && + request.method === 'GET') { + requestHandler.greet(request, response); + } + if (request.method === 'POST' && request.url === '/greet') { + requestHandler.greetJSON(request, response); + } }); server.listen(3000, function() { diff --git a/matthew_ringel/test/server_test.js b/matthew_ringel/test/server_test.js new file mode 100644 index 0000000..1bde3b8 --- /dev/null +++ b/matthew_ringel/test/server_test.js @@ -0,0 +1,13 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../index'); + +describe('our server', function() { + + it('should respond to a request to /time', function() { + + } ) +}) From 96408b317f0cd663f956c5ea883588f3fabd26d3 Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Wed, 4 Nov 2015 11:16:28 -0800 Subject: [PATCH 6/8] passing three tests --- matthew_ringel/test/server_test.js | 37 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/matthew_ringel/test/server_test.js b/matthew_ringel/test/server_test.js index 1bde3b8..a362932 100644 --- a/matthew_ringel/test/server_test.js +++ b/matthew_ringel/test/server_test.js @@ -7,7 +7,36 @@ require(__dirname + '/../index'); describe('our server', function() { - it('should respond to a request to /time', function() { - - } ) -}) + it('should respond to a GET request to /time', function(done) { + chai.request('localhost:3000') + .get('/time') + .end(function(error, response) { + expect(error).to.eql(null); + expect(response).to.have.status(200); + done(); + }); + }); + + it('should respond to a GET request to /greet/[name]', function(done) { + chai.request('localhost:3000') + .get('/greet/name') + .end(function(error, response) { + expect(error).to.eql(null); + expect(response).to.have.status(200); + done(); + }); + }); + + it('should respond to a POST request to /greet with JSON data', function(done) { + chai.request('localhost:3000') + .post('/greet') + .send({"name": "bob"}) + .end(function(error, response) { + expect(error).to.eql(null); + expect(response).to.have.status(200); + done(); + }); + }); + + +}); From a5984159d1f10245bbd2ff3ea2303950236d481a Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Wed, 4 Nov 2015 12:08:19 -0800 Subject: [PATCH 7/8] html index done --- matthew_ringel/gulpfile.js | 49 ++++++++++++++++++++++++++++ matthew_ringel/lib/requesthandler.js | 12 +++---- matthew_ringel/lib/server.js | 9 +++++ matthew_ringel/package.json | 2 ++ matthew_ringel/public/index.html | 16 +++++++++ matthew_ringel/test/server_test.js | 1 - 6 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 matthew_ringel/gulpfile.js diff --git a/matthew_ringel/gulpfile.js b/matthew_ringel/gulpfile.js new file mode 100644 index 0000000..a031142 --- /dev/null +++ b/matthew_ringel/gulpfile.js @@ -0,0 +1,49 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['index.js', 'lib/**/*.js', 'bin/**/*.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, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('mocha', function(){ + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true + } + })) + .pipe(mocha({reporter: 'spec'})); +}); + + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint', 'mocha']); diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js index a925ce6..8f09cbb 100644 --- a/matthew_ringel/lib/requesthandler.js +++ b/matthew_ringel/lib/requesthandler.js @@ -11,23 +11,23 @@ function greet(request, response) { function time(request, response) { var date = new Date(); - var time = date.toUTCString(); + var returnTime = date.toUTCString(); response.writeHead(200, {"Content-Type": "text/plain"}); - response.write(time); + response.write(returnTime); response.end(); } function greetJSON(request, response) { - console.log('handling POST greet with JSON'); + // console.log('handling POST greet with JSON'); var postData = ''; request.on('data', function (data) { postData += data; }); request.on('end', function() { var postName = JSON.parse(postData); - console.log('Received JSON: '); - console.dir(postName); - console.log(postName.name); + // console.log('Received JSON: '); + // console.dir(postName); + // console.log(postName.name); var name = postName.name; var body = "hello " + name.toString(); diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js index 4ba8624..ea70d09 100644 --- a/matthew_ringel/lib/server.js +++ b/matthew_ringel/lib/server.js @@ -1,9 +1,18 @@ var http = require('http'); var url = require('url'); +var fs = require('fs'); var requestHandler = require('./requesthandler'); var server = http.createServer(function(request, response) { + + if (request.url === '/') { + response.writeHead(200, {"Content-Type": "text/html"}); + data = fs.readFileSync(__dirname + '/../public/index.html'); + response.write(data || 'not found'); + response.end(); + } + if (request.url === '/time') { requestHandler.time(request, response); } diff --git a/matthew_ringel/package.json b/matthew_ringel/package.json index 5fc73a2..7f7e653 100644 --- a/matthew_ringel/package.json +++ b/matthew_ringel/package.json @@ -27,6 +27,8 @@ "chai": "^3.4.0", "chai-http": "^1.0.0", "gulp": "^3.9.0", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3", "mocha": "^2.3.3" } } diff --git a/matthew_ringel/public/index.html b/matthew_ringel/public/index.html index 5f226d5..9fb75cb 100644 --- a/matthew_ringel/public/index.html +++ b/matthew_ringel/public/index.html @@ -6,5 +6,21 @@

Routes

+
    +
  • /time
  • +
      +
    • returns the system time of the server in GMT
    • +
    +
  • /greet/[name]
  • +
      +
    • returns a greeting to [name]
    • +
    +
  • POST to /greet
  • +
      +
    • Expects a POST request to /greet sending JSON formatted as + {name: [name]} where [name] is a string
    • +
    • returns a greeting to [name]
    • +
    +
diff --git a/matthew_ringel/test/server_test.js b/matthew_ringel/test/server_test.js index a362932..1667122 100644 --- a/matthew_ringel/test/server_test.js +++ b/matthew_ringel/test/server_test.js @@ -38,5 +38,4 @@ describe('our server', function() { }); }); - }); From a40d0c1b453882a271c1ada363257e56bd3612da Mon Sep 17 00:00:00 2001 From: Matthew Ringel Date: Wed, 4 Nov 2015 12:16:35 -0800 Subject: [PATCH 8/8] added to README --- matthew_ringel/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/matthew_ringel/README.md b/matthew_ringel/README.md index 0d9c643..cb24bc0 100644 --- a/matthew_ringel/README.md +++ b/matthew_ringel/README.md @@ -1,8 +1,9 @@ #Vanilla Http Server -**Matthew Ringel** -**Code Fellows sea-d45-javascript** -**4 November 2015** +**Matthew Ringel** +**Code Fellows sea-d45-javascript** +**4 November 2015** +A Vanilla HTTP server with index.html, /greet/[name], /time, and a POST response from /greet that accepts a JSON file. @@ -13,4 +14,5 @@ +Some code borrowed from here: http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js