diff --git a/ChrisPerez/.gitignore b/ChrisPerez/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/ChrisPerez/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/ChrisPerez/README.md similarity index 100% rename from README.md rename to ChrisPerez/README.md diff --git a/ChrisPerez/gulpfile.js b/ChrisPerez/gulpfile.js new file mode 100644 index 0000000..348e6c5 --- /dev/null +++ b/ChrisPerez/gulpfile.js @@ -0,0 +1,61 @@ +'use strict'; +const gulp = require('gulp'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +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('default', ['test','lint'], () => { +}); + +gulp.task('test', () => { + gulp.src(__dirname + '/test/test.js') + .pipe(mocha()); +}); + +gulp.task('lint', () => { + gulp.src(__dirname + '/**.js') + .pipe(eslint(opts)) + .pipe(eslint.format()); +}); + +gulp.watch(__dirname + '/**/**.js',['test','lint']); diff --git a/ChrisPerez/package.json b/ChrisPerez/package.json new file mode 100644 index 0000000..9b7e59c --- /dev/null +++ b/ChrisPerez/package.json @@ -0,0 +1,28 @@ +{ + "name": "http-server-ckperez", + "version": "1.0.0", + "description": "##Description", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ckperez/basic_http_server.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ckperez/basic_http_server/issues" + }, + "homepage": "https://github.com/ckperez/basic_http_server#readme", + "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" + } +} diff --git a/ChrisPerez/server.js b/ChrisPerez/server.js new file mode 100644 index 0000000..aaf93e9 --- /dev/null +++ b/ChrisPerez/server.js @@ -0,0 +1,38 @@ +'use strict'; + +const http = require('http'); + +http.createServer((req, res)=>{ + var paramArray = req.url.split('/'); + + if (paramArray[1] === 'time'){ + res.write(new Date().toString() + '\n'); + return res.end(); + }; + + if (paramArray[1] === 'greet' && paramArray[2]){ + res.write(`Wuddup, ${paramArray[2]}\n`); + return res.end(); + } + + if (paramArray[1] === 'greet' && req.method === 'POST'){ + let name = ''; + req.on('data', (data)=>{ + name += JSON.parse(data).name; + }) + req.on('end', ()=>{ + console.log(name); + res.end(); + }) + res.writeHead(200, {'Content-Type': 'application/json'}); + res.write('neat.\n'); + return + } + + res.writeHead(404, { + 'Content-Type': 'text/html' + }) + res.write('NOT FOUND\n'); + res.end(); + +}).listen(3000); diff --git a/ChrisPerez/test/test.js b/ChrisPerez/test/test.js new file mode 100644 index 0000000..8fc58e1 --- /dev/null +++ b/ChrisPerez/test/test.js @@ -0,0 +1,45 @@ +const chai = require('chai'); +const chaiHTTP = require('chai-http'); +const expect = chai.expect; +chai.use(chaiHTTP); +const request = chai.request; +require(__dirname + '/../server.js'); + +describe('HTTP tests', ()=>{ + it('should greet by name based on url', (done)=>{ + request('localhost:3000') + .get('/greet/me') + .end((err, res)=>{ + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('Wuddup, me\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\n'); + done(); + }) + }) + it('should give me the current date/time', ()=>{ + request('localhost:3000') + .get('/time') + .end((err, res)=>{ + expect(err).to.eql(null); + expect(res.text.slice(0,20)).to.eql(new Date().toString().slice(0,20)) + }) + }) + it('should grab the name from a json object', ()=>{ + request('localhost:3000/greet') + .post('/greet') + .send('{"name": "person"}') + .end((err, res)=>{ + expect(res).to.have.status(200); + expect(JSON.parse(res.body.name)).to.eql('person'); + }) + }) +})