From 50de5497a6b5931c56c9e2c1a6224544bed8ada0 Mon Sep 17 00:00:00 2001 From: Rick Patci Date: Wed, 4 Nov 2015 12:17:17 -0800 Subject: [PATCH 1/2] first commit --- rick_patci/.gitignore | 1 + rick_patci/gulpfile.js | 30 ++++++++++++++++++++++++++++++ rick_patci/index.js | 0 rick_patci/lib/index.html | 16 ++++++++++++++++ rick_patci/lib/style.css | 3 +++ rick_patci/package.json | 16 ++++++++++++++++ rick_patci/server.js | 37 +++++++++++++++++++++++++++++++++++++ rick_patci/test/test.js | 0 8 files changed, 103 insertions(+) create mode 100644 rick_patci/.gitignore create mode 100644 rick_patci/gulpfile.js create mode 100644 rick_patci/index.js create mode 100644 rick_patci/lib/index.html create mode 100644 rick_patci/lib/style.css create mode 100644 rick_patci/package.json create mode 100644 rick_patci/server.js create mode 100644 rick_patci/test/test.js diff --git a/rick_patci/.gitignore b/rick_patci/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/rick_patci/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/rick_patci/gulpfile.js b/rick_patci/gulpfile.js new file mode 100644 index 0000000..7d9825f --- /dev/null +++ b/rick_patci/gulpfile.js @@ -0,0 +1,30 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var chaihttp = require('chai-http'); +var testFiles = ['test/**/*.js']; +var appFiles = ['index.js']; + +gulp.task('jshint:test', function() { + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('jshint:app', function() { + return gulp.src(appFiles) + .pipe(jshint({ + node: true + })) + .pipe(jshint.reporter('default')); +}) + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint']); diff --git a/rick_patci/index.js b/rick_patci/index.js new file mode 100644 index 0000000..e69de29 diff --git a/rick_patci/lib/index.html b/rick_patci/lib/index.html new file mode 100644 index 0000000..bf6bc65 --- /dev/null +++ b/rick_patci/lib/index.html @@ -0,0 +1,16 @@ + + + + + + +

Here are your options:

+

http://localhost:3000/time

+

http://localhost:3000/greet

+

http://localhost:3000/greet/yournamehere

+ + diff --git a/rick_patci/lib/style.css b/rick_patci/lib/style.css new file mode 100644 index 0000000..66acf11 --- /dev/null +++ b/rick_patci/lib/style.css @@ -0,0 +1,3 @@ +p { + outline-color: red; +} diff --git a/rick_patci/package.json b/rick_patci/package.json new file mode 100644 index 0000000..33b5272 --- /dev/null +++ b/rick_patci/package.json @@ -0,0 +1,16 @@ +{ + "name": "rick_http_server", + "version": "0.1.0", + "description": "simple http server", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Rick Patci", + "license": "MIT", + "devDependencies": { + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "gulp-jshint": "^1.12.0" + } +} diff --git a/rick_patci/server.js b/rick_patci/server.js new file mode 100644 index 0000000..ea4c6b2 --- /dev/null +++ b/rick_patci/server.js @@ -0,0 +1,37 @@ +var http = require('http'); +var PORT = 3000; +var fs = require('fs'); + +var routesAvailable = { + root: '/', + currentTime: '/time', + greet: '/greet', + greetName: '/greet/yourname' +}; + +var server = http.createServer(function(req, res) { + var resData = {}; + if(req.url === routesAvailable.root && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/html'; + resData.data = fs.readFileSync(__dirname + '/lib/index.html').toString(); + } + if(req.url === routesAvailable.currentTime && req.method === 'GET') { + resData.status = 200; + resData.contentType = 'text/plain'; + resData.data = new Date().getTime().toString(); + } + res.writeHead(resData.status || 400, { + 'Content-Type' : resData.contentType || 'text/plain' + }); + res.write(resData.data || 'not found'); + res.end(); +}); + +server.listen(PORT, function() { + console.log('Server listening on http://localhost:%s', PORT); +}); + +/* The server should respond to a request to /time that will send back +the current time of the server. */ + diff --git a/rick_patci/test/test.js b/rick_patci/test/test.js new file mode 100644 index 0000000..e69de29 From 21bbc7c9198fad6ca6709b1a27bf31d6f8c58a9d Mon Sep 17 00:00:00 2001 From: Rick Patci Date: Wed, 4 Nov 2015 13:52:15 -0800 Subject: [PATCH 2/2] fix test and css --- rick_patci/lib/index.html | 6 +----- rick_patci/lib/style.css | 2 +- rick_patci/server.js | 17 ++++++++++++++--- rick_patci/test/test.js | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/rick_patci/lib/index.html b/rick_patci/lib/index.html index bf6bc65..7ec2bc6 100644 --- a/rick_patci/lib/index.html +++ b/rick_patci/lib/index.html @@ -1,13 +1,9 @@ + -

Here are your options:

http://localhost:3000/time

http://localhost:3000/greet

diff --git a/rick_patci/lib/style.css b/rick_patci/lib/style.css index 66acf11..c861f36 100644 --- a/rick_patci/lib/style.css +++ b/rick_patci/lib/style.css @@ -1,3 +1,3 @@ p { - outline-color: red; + background-color: red; } diff --git a/rick_patci/server.js b/rick_patci/server.js index ea4c6b2..dfc17de 100644 --- a/rick_patci/server.js +++ b/rick_patci/server.js @@ -11,15 +11,26 @@ var routesAvailable = { var server = http.createServer(function(req, res) { var resData = {}; + if(req.url === routesAvailable.root && req.method === 'GET') { resData.status = 200; resData.contentType = 'text/html'; resData.data = fs.readFileSync(__dirname + '/lib/index.html').toString(); } + if(req.url === '/style.css') { + res.writeHead(200, { + 'Content-Type': 'text/css' + }); + res.write(fs.readFileSync(__dirname + '/lib/style.css')); + return res.end(); + } if(req.url === routesAvailable.currentTime && req.method === 'GET') { - resData.status = 200; - resData.contentType = 'text/plain'; - resData.data = new Date().getTime().toString(); + resData.status = 200; + resData.contentType = 'text/plain'; + resData.data = new Date().getTime().toString(); + } + if(req.url === routesAvailable.greet && req.method === 'POST') { + } res.writeHead(resData.status || 400, { 'Content-Type' : resData.contentType || 'text/plain' diff --git a/rick_patci/test/test.js b/rick_patci/test/test.js index e69de29..569e3f9 100644 --- a/rick_patci/test/test.js +++ b/rick_patci/test/test.js @@ -0,0 +1,24 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../server.js'); + +describe('the server', function() { + before(function() { + this.indexFileString = fs.readFileSync(__dirname + '/../lib/index.html').toString(); + }); + + it('should be able to get an index', function(done) { + chai.request('localhost:3000') + .get('/') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql(this.indexFileString); + console.log(res); + done(); + }.bind(this)); //uses a node-style callback. Angular uses promise-style callback + }); +});