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 ChrisPerez/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
File renamed without changes.
61 changes: 61 additions & 0 deletions ChrisPerez/gulpfile.js
Original file line number Diff line number Diff line change
@@ -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']);
28 changes: 28 additions & 0 deletions ChrisPerez/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
38 changes: 38 additions & 0 deletions ChrisPerez/server.js
Original file line number Diff line number Diff line change
@@ -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);
45 changes: 45 additions & 0 deletions ChrisPerez/test/test.js
Original file line number Diff line number Diff line change
@@ -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');
})
})
})