From 9210b934eb96b133f1902071f755ea4610a8fa50 Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Tue, 3 Nov 2015 19:07:42 -0800 Subject: [PATCH 1/7] initial commit --- spencer_caldwell/index.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 spencer_caldwell/index.html diff --git a/spencer_caldwell/index.html b/spencer_caldwell/index.html new file mode 100644 index 0000000..e69de29 From 5e43ce7ba2e68220cd1516d98f1fa5d5e2eb5cbe Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Tue, 3 Nov 2015 19:33:10 -0800 Subject: [PATCH 2/7] created structure --- spencer_caldwell/README.md | 27 +++++++++++++++++++++++++++ spencer_caldwell/app/index.html | 0 spencer_caldwell/app/script.js | 0 spencer_caldwell/app/style.css | 0 spencer_caldwell/server.js | 0 spencer_caldwell/test/server_test.js | 0 6 files changed, 27 insertions(+) create mode 100644 spencer_caldwell/README.md create mode 100644 spencer_caldwell/app/index.html create mode 100644 spencer_caldwell/app/script.js create mode 100644 spencer_caldwell/app/style.css create mode 100644 spencer_caldwell/server.js create mode 100644 spencer_caldwell/test/server_test.js diff --git a/spencer_caldwell/README.md b/spencer_caldwell/README.md new file mode 100644 index 0000000..82f1087 --- /dev/null +++ b/spencer_caldwell/README.md @@ -0,0 +1,27 @@ +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/spencer_caldwell/app/index.html b/spencer_caldwell/app/index.html new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/app/script.js b/spencer_caldwell/app/script.js new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/app/style.css b/spencer_caldwell/app/style.css new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/server.js b/spencer_caldwell/server.js new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/test/server_test.js b/spencer_caldwell/test/server_test.js new file mode 100644 index 0000000..e69de29 From 865f07450598458fdde7a5b38d3087161962265d Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Tue, 3 Nov 2015 19:45:12 -0800 Subject: [PATCH 3/7] server up --- README.md | 27 --------------------------- spencer_caldwell/server.js | 10 ++++++++++ 2 files changed, 10 insertions(+), 27 deletions(-) delete mode 100644 README.md 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/spencer_caldwell/server.js b/spencer_caldwell/server.js index e69de29..b8f62e9 100644 --- a/spencer_caldwell/server.js +++ b/spencer_caldwell/server.js @@ -0,0 +1,10 @@ +var http = require('http'); +var fs = require('fs'); + +var server = http.createServer(function(request, response) { + +}); + +server.listen(3000, function() { + console.log('server up'); +}) From 399713f52b85c82f2ae705bcb02ffd12945db579 Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Wed, 4 Nov 2015 12:19:58 -0800 Subject: [PATCH 4/7] all the paths work ... but its ugly ... and one test works --- .gitignore | 1 + spencer_caldwell/app/index.html | 0 spencer_caldwell/index.html | 0 spencer_caldwell/lib/time.js | 10 +++ spencer_caldwell/package.json | 19 ++++++ spencer_caldwell/public/index.html | 12 ++++ spencer_caldwell/{app => public}/script.js | 0 spencer_caldwell/{app => public}/style.css | 0 spencer_caldwell/server.js | 72 +++++++++++++++++++++- spencer_caldwell/test/server_test.js | 24 ++++++++ 10 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 .gitignore delete mode 100644 spencer_caldwell/app/index.html delete mode 100644 spencer_caldwell/index.html create mode 100644 spencer_caldwell/lib/time.js create mode 100644 spencer_caldwell/package.json create mode 100644 spencer_caldwell/public/index.html rename spencer_caldwell/{app => public}/script.js (100%) rename spencer_caldwell/{app => public}/style.css (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf70988 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/node_modules diff --git a/spencer_caldwell/app/index.html b/spencer_caldwell/app/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/spencer_caldwell/index.html b/spencer_caldwell/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/spencer_caldwell/lib/time.js b/spencer_caldwell/lib/time.js new file mode 100644 index 0000000..ee961ce --- /dev/null +++ b/spencer_caldwell/lib/time.js @@ -0,0 +1,10 @@ +module.exports = function() { + var date = Date.now(); + // var hours = date.getHours(); + // var minutes = '0' + date.getMinutes(); + // var seconds = '0' + date.getSeconds(); + // var formattedTime = hours + ':' + minutes.subtr(-2) + ':' + seconds.subtr(-2); + // formattedTimeString = formattedTime.toString(); + var dateString = date.toString(); + return dateString; +}; diff --git a/spencer_caldwell/package.json b/spencer_caldwell/package.json new file mode 100644 index 0000000..f3539c7 --- /dev/null +++ b/spencer_caldwell/package.json @@ -0,0 +1,19 @@ +{ + "name": "spencer_caldwell", + "version": "1.0.0", + "description": "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", + "main": "server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0" + } +} diff --git a/spencer_caldwell/public/index.html b/spencer_caldwell/public/index.html new file mode 100644 index 0000000..66d239c --- /dev/null +++ b/spencer_caldwell/public/index.html @@ -0,0 +1,12 @@ + + + + + My Super Awesome Site + + +

If you go to /greet/(some name), the server will print out hello and the name

+

If you go to /time, the server will send you its time in Unix time in milliseconds

+

If you submit a POST request to /greet, the server will take a name in JSON format

+ + diff --git a/spencer_caldwell/app/script.js b/spencer_caldwell/public/script.js similarity index 100% rename from spencer_caldwell/app/script.js rename to spencer_caldwell/public/script.js diff --git a/spencer_caldwell/app/style.css b/spencer_caldwell/public/style.css similarity index 100% rename from spencer_caldwell/app/style.css rename to spencer_caldwell/public/style.css diff --git a/spencer_caldwell/server.js b/spencer_caldwell/server.js index b8f62e9..2f5b806 100644 --- a/spencer_caldwell/server.js +++ b/spencer_caldwell/server.js @@ -1,10 +1,80 @@ var http = require('http'); var fs = require('fs'); +var time = require(__dirname + '/lib/time'); + +var server = http.createServer(function(req, res) { + var resData = {}; + var reqHeader = req.url; + var reqHeadStr = reqHeader.slice(7); + + if (req.url === '/' && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = fs.readFileSync(__dirname + '/public/index.html').toString(); +res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain'}); + res.write(resData.data || 'not found'); + res.end(); + + } + + else if (req.url === '/time' && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = time(); + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain'}); + res.write(resData.data || 'not found'); + res.end(); + } + + else if (req.url === '/greet/' + reqHeadStr && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = 'Hello ' + reqHeadStr + '!'; + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain'}); + res.write(resData.data || 'not found'); + res.end(); + } + +//code in question + + + + else if (req.url === '/greet' && req.method === 'POST') { + var parse = ''; + req.on('data', function(data) { + parse = JSON.parse(data); + }); + req.on('end', function() { + resData.status = 200; + resData.contentType = 'text/html'; + console.log(parse.name); + resData.data = parse.name; + res.writeHead(resData.status || 404, { + 'Content-Type': resData.contentType || 'text/plain'}); + res.write(resData.data || 'not found'); + res.end(); + + }); + } + + else { +res.writeHead(404, { + 'Content-Type': 'text/plain' + }); + res.write('page not found'); + res.end(); +} + +//end of code in question -var server = http.createServer(function(request, response) { }); server.listen(3000, function() { console.log('server up'); }) + +debugger; diff --git a/spencer_caldwell/test/server_test.js b/spencer_caldwell/test/server_test.js index e69de29..4cc6187 100644 --- a/spencer_caldwell/test/server_test.js +++ b/spencer_caldwell/test/server_test.js @@ -0,0 +1,24 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../server'); + +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)); + }); + +}); From 66f8eec45c05550b3570827cf5d9213f2b6156aa Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Wed, 30 Dec 2015 20:27:30 -0800 Subject: [PATCH 5/7] moved .gitignore file into spencer_caldwell directory --- .gitignore => spencer_caldwell/.gitignore | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .gitignore => spencer_caldwell/.gitignore (100%) diff --git a/.gitignore b/spencer_caldwell/.gitignore similarity index 100% rename from .gitignore rename to spencer_caldwell/.gitignore From 450e1892bbbf28e99a0705a392dc83529ee67b66 Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Wed, 30 Dec 2015 21:46:48 -0800 Subject: [PATCH 6/7] added 2 more tests --- spencer_caldwell/gulpfile.js | 0 spencer_caldwell/package.json | 6 +++++- spencer_caldwell/test/server_test.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spencer_caldwell/gulpfile.js diff --git a/spencer_caldwell/gulpfile.js b/spencer_caldwell/gulpfile.js new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/package.json b/spencer_caldwell/package.json index f3539c7..0ae9a43 100644 --- a/spencer_caldwell/package.json +++ b/spencer_caldwell/package.json @@ -14,6 +14,10 @@ "license": "ISC", "devDependencies": { "chai": "^3.4.0", - "chai-http": "^1.0.0" + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "gulp-jshint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "jshint": "^2.8.0" } } diff --git a/spencer_caldwell/test/server_test.js b/spencer_caldwell/test/server_test.js index 4cc6187..87998e8 100644 --- a/spencer_caldwell/test/server_test.js +++ b/spencer_caldwell/test/server_test.js @@ -21,4 +21,26 @@ describe('our server', function() { }.bind(this)); }); + it('should respond to /time by sending back the current time', function(done) { + chai.request('localhost:3000') + .get('/time') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.be.above(1451540386398); + done(); + }); + }); + + it('should respond to /greet/* with the name you send it', function(done) { + chai.request('localhost:3000') + .get('/greet/test') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('Hello test!'); + done(); + }); + }); + }); From 19a919d64e82532f480eec7bbe75d58d92d08831 Mon Sep 17 00:00:00 2001 From: Spencer Caldwell Date: Wed, 30 Dec 2015 21:55:20 -0800 Subject: [PATCH 7/7] added gulpfile --- spencer_caldwell/gulpfile.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spencer_caldwell/gulpfile.js b/spencer_caldwell/gulpfile.js index e69de29..01ac68c 100644 --- a/spencer_caldwell/gulpfile.js +++ b/spencer_caldwell/gulpfile.js @@ -0,0 +1,23 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var serverFiles = ['server.js']; +var testFiles = ['test/*.js']; + +gulp.task('jshint:test', function() { + return gulp.src(testFiles) + .pipe(jshint.reporter('default')); +}); + +gulp.task('jshint:server', function() { + return gulp.src(serverFiles) + .pipe(jshint.reporter('default')); +}); + +gulp.task('mocha', function() { + return gulp.src('./test/*.js', {read: true}) + .pipe(mocha({reporter: 'nyan'})); +}); + +gulp.task('jshint', ['jshint:test', 'jshint:server']); +gulp.task('default', ['jshint', 'mocha']);