-
Notifications
You must be signed in to change notification settings - Fork 12
Aliza lab #8
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?
Aliza lab #8
Changes from all commits
ec9300a
dbfc9f3
ce3757d
a235cf6
4f3fa34
78a5c0e
c34c4dd
75959fb
4d90767
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,19 @@ | ||
| const gulp = require('gulp'); | ||
| const eslint = require('gulp-eslint'); | ||
| const mocha = require ('gulp-mocha'); | ||
|
|
||
| gulp.task('default', ['lint', 'mocha'], () => { | ||
| console.log('default for lint and mocha'); | ||
| }); | ||
|
|
||
|
|
||
| gulp.task('lint', () => { | ||
| gulp.src('/*.js') | ||
| .pipe(eslint({})) | ||
| .pipe(eslint.format()) | ||
| }); | ||
|
|
||
| gulp.task('mocha', () => { | ||
| gulp.src('test/test.js') | ||
| .pipe(mocha()); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "name": "aliza", | ||
| "version": "1.0.0", | ||
| "description": "basic http server lab", | ||
| "main": "server.js", | ||
| "directories": { | ||
| "test": "test" | ||
| }, | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "start": "node server.js" | ||
| }, | ||
| "author": "aliza", | ||
| "license": "ISC", | ||
| "devDependencies": { | ||
| "chai": "^3.5.0", | ||
| "chai-http": "^2.0.1", | ||
| "gulp": "^3.9.1", | ||
| "gulp-eslint": "^2.0.0", | ||
| "gulp-mocha": "^2.2.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
|
|
||
| const database = {}; | ||
|
|
||
| http.createServer((req, res) => { | ||
| if (req.url === '/time' && req.method === 'GET'){ | ||
| let date = new Date(); | ||
| let time = date.getHours() + ':' + date.getMinutes(); | ||
| res.write(time + '\n'); | ||
| return res.end(); | ||
| } | ||
|
|
||
| let user = req.url.substring(7); | ||
| if (req.url === '/greet/' + user && req.method === 'GET'){ | ||
| res.write('Hello ' + user + '\n'); | ||
| return res.end(); | ||
| } | ||
|
|
||
| //curl localhost:3000/greet -X POST -d '{"name":"aliza"}' | ||
|
|
||
| if (req.url === '/greet' && req.method === 'POST') { | ||
| var name = ''; | ||
| req.on('data', (data) => { | ||
| name += data.toString(); | ||
| }); | ||
| req.on('end', () => { | ||
| var nameObj = JSON.parse(name); | ||
| res.write('Hello ' + nameObj.name + '\n'); | ||
| return res.end(); | ||
| }) | ||
|
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. If you wanted to move to away from the if/then/else you would put a return after the last handler was registered (line 33). The idea is the your route assigns handlers and then leaves the create server code. Its done everything it needs to do by assigning the handlers. The return within the asynch handler code isn't really necessary since there is no code to execute in after it. POST routers rely on data that won't be available synchronously, so all they can do is register handler callbacks and then get out the of create server code. They don't want to end the res stream. |
||
| } | ||
|
|
||
| else { | ||
| res.writeHead(404, { | ||
| 'Content-Type': 'text/html' | ||
| }) | ||
| res.write('NOT FOUND'); | ||
| res.end(); | ||
| } | ||
|
|
||
| }).listen(3000); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const chai = require('chai'); | ||
| const chaiHTTP = require('chai-http'); | ||
| const expect = chai.expect; | ||
| chai.use(chaiHTTP); | ||
| const request = chai.request; | ||
|
|
||
| require('../server'); | ||
|
|
||
| describe('HTTP tests', () => { | ||
| it('should give time on get /time', (done) => { | ||
| request('localhost:3000') | ||
| .get('/time') | ||
| .end((err, res) => { | ||
| expect(err).to.eql(null); | ||
| expect(res).to.have.status(200); | ||
| expect(res.text).to.eql(new Date().getHours() + ':' + new Date().getMinutes() + '\n'); | ||
| done(); | ||
| }); | ||
| }); | ||
| // it('should catch not found', (done) => { | ||
| // request('localhost: 3000') | ||
| // .get('/notthere') | ||
| // .end((err, res) => { | ||
| // expect(res).to.have.status(404); | ||
| // expect(res.text).to.eql('NOT FOUND'); | ||
| // done(); | ||
| // }); | ||
| // }); | ||
| }); |
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.
I finally understand why you needed to declare user outside of the if statement. However, since there might not always be a character at substring(7) like when the route is just '/greet', user will be undefined, but that's OK because it doesn't cause an error, it just silently returns "undefined".