diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..25fa30d --- /dev/null +++ b/gulpfile.js @@ -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'); +}); diff --git a/http_server.js b/http_server.js new file mode 100644 index 0000000..88e621a --- /dev/null +++ b/http_server.js @@ -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'); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..94d18d0 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test/http_test.js b/test/http_test.js new file mode 100644 index 0000000..d91bac1 --- /dev/null +++ b/test/http_test.js @@ -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(); + }); + }); +});