diff --git a/jenny_pollack/.gitignore b/jenny_pollack/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/jenny_pollack/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/jenny_pollack/gulpfile.js b/jenny_pollack/gulpfile.js new file mode 100644 index 0000000..fc11fd9 --- /dev/null +++ b/jenny_pollack/gulpfile.js @@ -0,0 +1,49 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['index.js', 'lib/**/*.js', 'bin/**/*.js']; +var testFiles = ['test/**/*.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, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(jshint.reporter('default')); +}); + +gulp.task('mocha', function(){ + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true + } + })) + .pipe(mocha({reporter: 'spec'})); +}); + + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint', 'mocha']); \ No newline at end of file diff --git a/jenny_pollack/index.js b/jenny_pollack/index.js new file mode 100644 index 0000000..3a7f582 --- /dev/null +++ b/jenny_pollack/index.js @@ -0,0 +1,18 @@ +var server = require(__dirname + '/lib/server'); + +// It should also respond to a get request to /greet/name +// where name is any single word string. +// It should send back a string that greets that name. + +// It should also have a separate post request to /greet +// that takes the name in JSON format. + +// There should be tests using chaiHTTP for both routes, +// as well as a gulpfile/package.json + +// You should have an html page that describes the +// routes implemented by the api available + +// at the root of the server (for a bonus point +// auto populate the routes list, for another +// bonus point also style the html page) \ No newline at end of file diff --git a/jenny_pollack/lib/server.js b/jenny_pollack/lib/server.js new file mode 100644 index 0000000..fcbef43 --- /dev/null +++ b/jenny_pollack/lib/server.js @@ -0,0 +1,95 @@ +var http = require('http'); +var fs = require('fs'); +var url = require('url'); + + +var server = http.createServer(function(request, response){ + var responseData = {}; + + var pathname = url.parse(request.url).pathname; + var arr = pathname.split('/'); + + console.log('request url: ' + request.url); + + if(request.url === '/' && request.method === 'GET'){ + responseData.status = 200; + responseData.contentType = 'text/html'; + responseData.data = fs.readFileSync(__dirname + '/../public/index.html').toString(); + + response.writeHead(responseData.status || '404', { + 'Content-Type': responseData.contentType || 'text/plain' + }); + response.write(responseData.data || 'not found'); + response.end(); + } + + if(request.url === '/time' && request.method === 'GET'){ + var date = new Date(); + console.log('time: ' + date); + responseData.status = 200; + responseData.contentType = 'text/html'; + responseData.data = date.toString(); + + response.writeHead(responseData.status || '404', { + 'Content-Type': responseData.contentType || 'text/plain' + }); + + response.write(responseData.data || 'not found'); + response.end(); + } + + if(arr[1] === 'greet' && request.method === 'GET'){ + if(arr.length === 3){ + responseData.status = 200; + responseData.contentType = 'text/html'; + responseData.data = 'Greetings, ' + arr[2]; + + response.writeHead(responseData.status || '404', { + 'Content-Type': responseData.contentType || 'text/plain' + }); + + response.write(responseData.data || 'not found'); + response.end(); + } else{ + responseData.data = 'Hello anon'; + + response.writeHead(responseData.status || '404', { + 'Content-Type': responseData.contentType || 'text/plain' + }); + + response.write(responseData.data || 'not found'); + response.end(); + } + } + + if(arr[1] === 'greet' && request.method === 'POST'){ + console.log('incoming post'); + + request.on('data', function(chunk) { + console.log("Received body data:"); + console.log(chunk.toString()); + + responseData.status = 200; + responseData.contentType = 'application/json'; + responseData.data = chunk.toString(); + }); + + request.on('end', function(){ + response.writeHead(responseData.status || '404', { + 'Content-Type': responseData.contentType || 'text/plain' + }); + + response.write(responseData.data || 'not found'); + response.end(); + + }); + } +}); + + + +server.listen(3000, function(){ + console.log('server running'); +}); + + diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json new file mode 100644 index 0000000..46cf720 --- /dev/null +++ b/jenny_pollack/package.json @@ -0,0 +1,26 @@ +{ + "name": "create_an_http_server", + "version": "0.1.0", + "description": "http server in vanilla node that responds to several different routes.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/jennypollack/create_an_http_server.git" + }, + "author": "Jenny Pollack", + "license": "MIT", + "bugs": { + "url": "https://github.com/jennypollack/create_an_http_server/issues" + }, + "homepage": "https://github.com/jennypollack/create_an_http_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp-mocha": "^2.1.3", + "jshint": "^2.8.0", + "mocha": "^2.3.3" + } +} diff --git a/jenny_pollack/public/index.html b/jenny_pollack/public/index.html new file mode 100644 index 0000000..07ae6ad --- /dev/null +++ b/jenny_pollack/public/index.html @@ -0,0 +1,21 @@ + + + + + My Super Awesome Site + + +

Wow, here is an h1, so h1, much tag

+ You should have an html page that describes the + routes implemented by the api available +

Routes Available

+ + + + + + diff --git a/jenny_pollack/test/http_test.js b/jenny_pollack/test/http_test.js new file mode 100644 index 0000000..9f4a48e --- /dev/null +++ b/jenny_pollack/test/http_test.js @@ -0,0 +1,57 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); + +//we don't store the server in a var we just +//start the server? +require(__dirname + '/../lib/server'); + + + +describe('an http request', function() { + it('should have status 200', function() { + chai.request('http://localhost:3000') + .get('/') + .then(function (res) { + console.log(res); + expect(res).to.have.status(200); + }); + }); +}); + + +describe('our server', function() { + before(function() { + this.indexFileString = fs.readFileSync(__dirname + '/../public/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); + done(); + }.bind(this)); + }); + after(function(){ + console.log('after'); + }) +}); + +describe('our server', function(){ + it('should be able to get the time', function(done){ + chai.request('localhost:3000') + .get('/time') + .end(function(err, res){ + expect(err).to.eql(null); + expect(res).to.have.status(200); + done(); + }); + }); +});