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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dustin/node_modules
.vscode
25 changes: 25 additions & 0 deletions dustin/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var mocha = require('gulp-mocha');
var watch = require('gulp-watch');

gulp.task('watch', function () {
gulp.watch(['*.js', '../test/*.js'], ['lint', 'mocha']);
});

gulp.task('default', ['mocha', 'lint', 'watch'], function () {});

gulp.task('lint', function () {
gulp.src(['./*.js', './test/*.js', './lib/*.js'])
.pipe(eslint()) //{} pass in rules
.pipe(eslint.format());
});

gulp.task('mocha', function () {
return gulp.src('./test/*.js', {
read: false
})
.pipe(mocha({
reporter: 'nyan'
}));
});
20 changes: 20 additions & 0 deletions dustin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "basic_http_server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"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",
"gulp-watch": "^4.3.5",
"mocha": "^2.4.5"
}
}
73 changes: 73 additions & 0 deletions dustin/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var http = require('http');
savedJSON = [];

http.createServer(function (req, res) {


res.processed = false;
url_array = req.url.split('/');
if (req.method == 'GET' && url_array[1] == 'time') urlTime(res);
if (req.method == 'GET' && url_array[1] == 'greet' && (url_array[2])) urlName(res);
if (req.method == 'POST' && url_array[1] == 'greet') urlNamePost(res, req);
if (res.processed === false) url404(res);

}).listen(3000);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare

url404 = function (res) {
res.writeHead(404, {
'Content-Type': 'text/html'
});
res.write('Not found');
res.end();
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your

urlTime = function (res) {
res.processed = true;
res.write((new Date).toString());
res.end();
return;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables

urlNamePost = function (res, req) {
res.processed = true;
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
if (req.headers['content-type'] === 'application/json') {
parseJSON(body);
goodRequest(res);
} else {
badRequest(res);
}
});
};

urlName = function (res) {
res.processed = true;
res.write('Hello, ' + url_array[2]);
res.end();
return;
};

badRequest = function (res) {
res.processed = true;
res.writeHead(400, {
'Content-Type': 'text/html'
});
res.write('BAD REQUEST');
res.end();
};

goodRequest = function (res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write('request accepted');
res.end();
};

parseJSON = function (body) {
savedJSON.push(JSON.parse(body));
};
63 changes: 63 additions & 0 deletions dustin/test/server_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
chai = require('chai');
chaiHTTP = require('chai-http');
expect = chai.expect;
chai.use(chaiHTTP);
request = chai.request;

require('../server');

//The server should respond to a request to /time that will send back
//the current time of the server.
describe('HTTP server', function () {
it('/time should return the system time (within 1 second)', function (done) {
request('localhost:3000')
.get('/time')
.end(function (err, res) {
expect(Date.now() - Date.parse(res.text)).to.be.below(1000);
expect(err).to.eql(null);
expect(res).to.have.status(200);
done();
});
});

//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('/greet/[name] should return a personal greet message', function (done) {
request('localhost:3000')
.get('/greet/jeffrey_lebowski')
.end(function (err, res) {
expect(res.text).to.eql('Hello, jeffrey_lebowski');
expect(err).to.eql(null);
expect(res).to.have.status(200);
done();
});
});

//It should also have a separate post request to /greet that takes the name in JSON format.
it('/greet should take a JSON post', function (done) {
request('localhost:3000')
.post('/greet')
.set('Content-Type', 'application/json')
.send({
'name': 'walter sobchak'
})
.end(function (err, res) {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(savedJSON).to.eql([{
'name': 'walter sobchak'
}]);
done();
});
});

it('/greet should take not take a non JSON post', function (done) {
request('localhost:3000')
.post('/greet')
.set('Content-Type', 'text/html')
.end(function (err, res) {
expect(res).to.have.status(400);
done();
});
});
});