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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
65 changes: 65 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const gulp = require('gulp');
const mocha = require('gulp-mocha');
const lint = require('gulp-eslint');
const opts = {
'extends': 'eslint:recommended',
'ecmaFeatures': {
'modules': true
},
'rules': {
'no-alert': 0,
'no-bitwise': 0,
'camelcase': 1,
'no-console': 1,
'curly': 1,
'eqeqeq': 0,
'no-eq-null': 0,
'guard-for-in': 1,
'no-empty': 1,
'no-use-before-define': 0,
'no-obj-calls': 2,
'no-unused-vars': 0,
'new-cap': 1,
'no-shadow': 0,
'strict': 1,
'no-invalid-regexp': 2,
'comma-dangle': 2,
'no-undef': 1,
'no-new': 1,
'no-extra-semi': 1,
'no-debugger': 2,
'no-caller': 1,
'semi': 1,
'quotes': 0,
'no-unreachable': 2
},
'globals': {
'$': false
},
'env': {
'node': true,
'es6': true
}
};

gulp.task('linter' , () => {
return gulp.src('./**/*.js')
.pipe(lint(opts))
.pipe(lint.format());
});

gulp.task('tests', () => {
return gulp.src('./test/http_test.js', {read: false})
.pipe(mocha({reporter: 'spec'}));
});

gulp.task('watch', () => {
gulp.watch('./test/http_test.js', ['linter', 'tests']);
gulp.watch('./**/*.js', ['linter']);
});

gulp.task('default', ['watch', 'linter', 'tests'], () => {
console.log('All tasks completed successfully');
});
37 changes: 37 additions & 0 deletions http_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const http = require('http');

http.createServer((req, res) => {
if (req.url === '/time') {
let date = new Date();
res.write('Current date: ' + date.toString() + '\n');
return res.end();
}

if (req.method === 'GET' && req.url.indexOf('/greet/') !== -1) {
let name = req.url.split('/').pop();
res.write('Hello ' + name);
return res.end();
}

if (req.method === 'POST' && req.url === '/greet') {
let body = '';
req.on('data', (data) => {
body += data.toString();
req.on('end', () => {
res.writeHead(200, {'Content-Type': 'text/html'});
let name = JSON.parse(body).name;
res.write('Name sent: ' + name);
return res.end();
})
});
}

else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write('NOT FOUND');
res.end();
}
}).listen(3000, () => {
console.log('listening');
});
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "stefanie-hansen",
"version": "1.0.0",
"description": "##Description",
"main": "gulpfile.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stefuhnee/basic_http_server.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/stefuhnee/basic_http_server/issues"
},
"homepage": "https://github.com/stefuhnee/basic_http_server#readme",
"dependencies": {},
"devDependencies": {
"chai": "^3.5.0",
"chai-http": "^2.0.1",
"gulp": "^3.9.1",
"gulp-eslint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"mocha": "^2.4.5",
"moment": "^2.13.0"
}
}
55 changes: 55 additions & 0 deletions test/http_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

const chai = require('chai');
const expect = require('chai').expect;
const chaiHTTP = require('chai-http');
chai.use(chaiHTTP);
const request = chai.request;
const moment = require('moment');
require('../http_server.js');

describe('HTTP server tests', () => {
it('should respond to a request with a url ending in /date with the current date', (done) => {
request('localhost:3000')
.get('/time')
.end((err, res) => {
expect(err).to.eql(null);
expect(res).to.have.status(200);
let dateString = res.text.split(':').slice(1).join('').trim();
expect(moment(dateString, 'ddd MMM DD YYYY HH:mm:ss').isValid()).to.eql(true);
done();
});
});
it('should respond to a GET request to /greet/* with a greeting including the single-word string at the end of the path', (done) => {
request('localhost:3000')
.get('/greet/test')
.end((err, res) => {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('Hello test');
done();
});
});
it('should respond to a POST request to /greet by recognizing the JSON object and writing it back to the server', (done) => {
request('localhost:3000')
.post('/greet')
.set('Content-Type', 'application/json')
.send({"name":"test"})
.end((err, res) => {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res).to.have.header('Content-Type', 'text/html');
expect(res.text).to.eql('Name sent: test');
done();
});
});
it('should respond to any other paths with an error', (done) => {
request('localhost:3000')
.get('/random')
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.text).to.eql('NOT FOUND');
done();
});
});
});