Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions aliza/gulpfile.js
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());
});
22 changes: 22 additions & 0 deletions aliza/package.json
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"
}
}
43 changes: 43 additions & 0 deletions aliza/server.js
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'){

Copy link
Copy Markdown

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".

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();
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
29 changes: 29 additions & 0 deletions aliza/test/test.js
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();
// });
// });
});