diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2032783 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules +data diff --git a/README.md b/robin_stringer/README.md similarity index 100% rename from README.md rename to robin_stringer/README.md diff --git a/robin_stringer/gulpfile.js b/robin_stringer/gulpfile.js new file mode 100644 index 0000000..d80e091 --- /dev/null +++ b/robin_stringer/gulpfile.js @@ -0,0 +1,43 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['server.js', './lib/**/*.js', 'bin/**/*.js']; +var testFiles = ['test/**/*.js']; + +gulp.task('mocha:test', function() { + return gulp.src(testFiles, appFiles) + .pipe(mocha({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(mocha({reporter: 'nyan'})); +}); + +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/robin_stringer/lib/index.html b/robin_stringer/lib/index.html new file mode 100644 index 0000000..069c2b4 --- /dev/null +++ b/robin_stringer/lib/index.html @@ -0,0 +1,20 @@ + + + + + + Routes implemented by API + + + +

Vanilla HTTP Server

+ + + + diff --git a/robin_stringer/lib/router.js b/robin_stringer/lib/router.js new file mode 100644 index 0000000..73e0e5a --- /dev/null +++ b/robin_stringer/lib/router.js @@ -0,0 +1,24 @@ + +'use strict' + +var fs = require('fs'); +var url = require('url'); + +// Respond to a request to /time that will send back the current time of the server. + +if(request.url === '/time' && request.method.toLowerCase() === 'get') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end(new Date().toString()); +} + +// Respond to a get request to /greet/name where name is any single word string. + +if(request.url === '/greet/name' && request.method.toLowerCase() === 'get') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end('Hello ' + name + '!'); +}); + +if(request.url === '/greet' && request.method.toLowerCase() === 'post') { + response.writeHead(200, {'Content-Type': 'text/plain'}); + response.end('Hello ' + name + '!'); +} diff --git a/robin_stringer/lib/server.js b/robin_stringer/lib/server.js new file mode 100644 index 0000000..c7aef92 --- /dev/null +++ b/robin_stringer/lib/server.js @@ -0,0 +1,11 @@ + +// Create web server +var router = require(__dirname + '/lib/route'); + +var http = require('http'); +http.createServer(function(request, response) { + homeRoute(request, response); +}).listen(3000); +console.log('Server up and running'); + +var server = http.createServer(function(request, res) diff --git a/robin_stringer/package.json b/robin_stringer/package.json new file mode 100644 index 0000000..d6eedcf --- /dev/null +++ b/robin_stringer/package.json @@ -0,0 +1,25 @@ +{ + "name": "robin_stringer", + "version": "0.0.1", + "description": "Vanilla HTTP Server", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rastringer/create_an_http_server.git" + }, + "author": "ra1stringer@gmail.com", + "license": "MIT", + "bugs": { + "url": "https://github.com/rastringer/create_an_http_server/issues" + }, + "homepage": "https://github.com/rastringer/create_an_http_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "jshint": "^2.8.0" + } +} diff --git a/robin_stringer/test/test.js b/robin_stringer/test/test.js new file mode 100644 index 0000000..4ea3807 --- /dev/null +++ b/robin_stringer/test/test.js @@ -0,0 +1,66 @@ + +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../server'); + +describe('GET', function() { + before(function() { + this.indexFile = fs.readFileSync(__dirname + '/../public/index.html').toString(); + }); + + it('should be able to get an index', function(done) { + debugger; + chai.request('localhost:3000') + .get('/') + .end(function(err, response) { + debugger; + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql(this.indexFileString); + done(); + }.bind(this)); + }); +}); + +describe('GET /time', function() { + it('should send back time: (PST)', function(done) { + chai.request('localhost:3000') + .get('/time') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text.split(' ').pop()).to.eql('(PST)'); + done(); + }); + }); +}); + +describe('GET greet/:name', function() { + it('should greet by name', function(done) { + chai.request('localhost:3000') + .get('/greet/test') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql('Hello!'); + done(); + }); + }); +}); + +describe('POST greet/', function() { + it('should greet your name', function(done) { + chai.request('localhost:3000') + .post('/greet') + .end(function(err, response) { + expect(err).to.eql(null); + expect(response).to.have.status(200); + expect(response.text).to.eql('Hello test!'); + done(); + }); + }); +}); +