-
Notifications
You must be signed in to change notification settings - Fork 20
Spencer #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Spencer #16
Changes from all commits
9210b93
5e43ce7
865f074
399713f
66f8eec
450e189
19a919d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| **/node_modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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']); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the readme, it should go in a readme, or another .txt document called "assignment" or something, not in the description field. |
||
| "main": "server.js", | ||
| "directories": { | ||
| "test": "test" | ||
| }, | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "start": "node server.js" | ||
| }, | ||
| "author": "", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Own your work! Fill out the author field :) |
||
| "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" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>My Super Awesome Site</title> | ||
| </head> | ||
| <body> | ||
| <p>If you go to /greet/(some name), the server will print out hello and the name</p> | ||
| <p>If you go to /time, the server will send you its time in Unix time in milliseconds</p> | ||
| <p>If you submit a POST request to /greet, the server will take a name in JSON format</p> | ||
| </body> | ||
| </html> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent instructions page. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabbing is a bit off here |
||
| } | ||
|
|
||
| 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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabbing again |
||
| 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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabbing again |
||
| res.end(); | ||
| } | ||
|
|
||
| //code in question | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this whitespace is not needed |
||
|
|
||
| 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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabbing again |
||
| res.end(); | ||
|
|
||
| }); | ||
| } | ||
|
|
||
| else { | ||
| res.writeHead(404, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tabbing again |
||
| '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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good styling here! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't commit commented-out code