diff --git a/README.md b/README.md deleted file mode 100644 index 82f1087..0000000 --- a/README.md +++ /dev/null @@ -1,27 +0,0 @@ -Vanilla HTTP Server -=========================== -To complete this assignment: - * fork this repository (the sub module for this specific assignment) - * clone down your fork - * place all of your work in a folder that is your full name, use `_`s instead of spaces - * push back up to your fork - * create a pull request back to the original repo - * submit a link to the PR in canvas - -Assingment Description ---------------------------- -For this assignment you should write an http server in vanilla node that responds to several different routes. - -The server should respond to a request to /time that will send back the current time of the 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 - - -Rubric: - * Tests: 4pts - * Routes: 4pts - * Organization and gulpfile/package.json 2pts diff --git a/matthew_ringel/.gitignore b/matthew_ringel/.gitignore new file mode 100644 index 0000000..2ed8d17 --- /dev/null +++ b/matthew_ringel/.gitignore @@ -0,0 +1 @@ +/node_modules/** diff --git a/matthew_ringel/README.md b/matthew_ringel/README.md new file mode 100644 index 0000000..cb24bc0 --- /dev/null +++ b/matthew_ringel/README.md @@ -0,0 +1,18 @@ +#Vanilla Http Server +**Matthew Ringel** +**Code Fellows sea-d45-javascript** +**4 November 2015** + +A Vanilla HTTP server with index.html, /greet/[name], /time, and a POST response from /greet that accepts a JSON file. + + + + + + + + + + +Some code borrowed from here: +http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js diff --git a/matthew_ringel/gulpfile.js b/matthew_ringel/gulpfile.js new file mode 100644 index 0000000..a031142 --- /dev/null +++ b/matthew_ringel/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']); diff --git a/matthew_ringel/index.js b/matthew_ringel/index.js new file mode 100644 index 0000000..affc685 --- /dev/null +++ b/matthew_ringel/index.js @@ -0,0 +1,2 @@ +require(__dirname + '/lib/requesthandler'); +require(__dirname + '/lib/server.js'); diff --git a/matthew_ringel/lib/requesthandler.js b/matthew_ringel/lib/requesthandler.js new file mode 100644 index 0000000..8f09cbb --- /dev/null +++ b/matthew_ringel/lib/requesthandler.js @@ -0,0 +1,42 @@ +var url = require('url'); +var qs = require('querystring'); + +function greet(request, response) { + var name = url.parse(request.url).pathname.split('/').pop(); + var body = "Hello " + name.toString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(body); + response.end(); +} + +function time(request, response) { + var date = new Date(); + var returnTime = date.toUTCString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(returnTime); + response.end(); +} + +function greetJSON(request, response) { + // console.log('handling POST greet with JSON'); + var postData = ''; + request.on('data', function (data) { + postData += data; + }); + request.on('end', function() { + var postName = JSON.parse(postData); + // console.log('Received JSON: '); + // console.dir(postName); + // console.log(postName.name); + + var name = postName.name; + var body = "hello " + name.toString(); + response.writeHead(200, {"Content-Type": "text/plain"}); + response.write(body); + response.end(); + }); +} + +exports.greet = greet; +exports.time = time; +exports.greetJSON = greetJSON; diff --git a/matthew_ringel/lib/server.js b/matthew_ringel/lib/server.js new file mode 100644 index 0000000..ea70d09 --- /dev/null +++ b/matthew_ringel/lib/server.js @@ -0,0 +1,31 @@ +var http = require('http'); +var url = require('url'); +var fs = require('fs'); + +var requestHandler = require('./requesthandler'); + +var server = http.createServer(function(request, response) { + + if (request.url === '/') { + response.writeHead(200, {"Content-Type": "text/html"}); + data = fs.readFileSync(__dirname + '/../public/index.html'); + response.write(data || 'not found'); + response.end(); + } + + if (request.url === '/time') { + requestHandler.time(request, response); + } + if (url.parse(request.url).pathname.split('/').length === 3 && + url.parse(request.url).pathname.split('/')[1] === 'greet' && + request.method === 'GET') { + requestHandler.greet(request, response); + } + if (request.method === 'POST' && request.url === '/greet') { + requestHandler.greetJSON(request, response); + } +}); + +server.listen(3000, function() { + console.log('server running on port 3000'); +}); diff --git a/matthew_ringel/package.json b/matthew_ringel/package.json new file mode 100644 index 0000000..7f7e653 --- /dev/null +++ b/matthew_ringel/package.json @@ -0,0 +1,34 @@ +{ + "name": "vanillahttp", + "version": "0.1.0", + "description": "vanilla http server for week 2 of sea-d45-javascript", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mringel/create_an_http_server.git" + }, + "keywords": [ + "http", + "node" + ], + "author": "matthew ringel", + "license": "MIT", + "bugs": { + "url": "https://github.com/mringel/create_an_http_server/issues" + }, + "homepage": "https://github.com/mringel/create_an_http_server#readme", + "devDependencies": { + "chai": "^3.4.0", + "chai-http": "^1.0.0", + "gulp": "^3.9.0", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3", + "mocha": "^2.3.3" + } +} diff --git a/matthew_ringel/public/index.html b/matthew_ringel/public/index.html new file mode 100644 index 0000000..9fb75cb --- /dev/null +++ b/matthew_ringel/public/index.html @@ -0,0 +1,26 @@ + + +
+ +