diff --git a/spencer_caldwell/.gitignore b/spencer_caldwell/.gitignore new file mode 100644 index 0000000..cf70988 --- /dev/null +++ b/spencer_caldwell/.gitignore @@ -0,0 +1 @@ +**/node_modules diff --git a/README.md b/spencer_caldwell/README.md similarity index 100% rename from README.md rename to spencer_caldwell/README.md diff --git a/spencer_caldwell/gulpfile.js b/spencer_caldwell/gulpfile.js new file mode 100644 index 0000000..01ac68c --- /dev/null +++ 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']); 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..0ae9a43 --- /dev/null +++ b/spencer_caldwell/package.json @@ -0,0 +1,23 @@ +{ + "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", + "gulp": "^3.9.0", + "gulp-jshint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "jshint": "^2.8.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/public/script.js b/spencer_caldwell/public/script.js new file mode 100644 index 0000000..e69de29 diff --git a/spencer_caldwell/public/style.css b/spencer_caldwell/public/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..2f5b806 --- /dev/null +++ b/spencer_caldwell/server.js @@ -0,0 +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 + + +}); + +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 new file mode 100644 index 0000000..87998e8 --- /dev/null +++ b/spencer_caldwell/test/server_test.js @@ -0,0 +1,46 @@ +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)); + }); + + 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(); + }); + }); + +});