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
27 changes: 0 additions & 27 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions matthew_ringel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/**
18 changes: 18 additions & 0 deletions matthew_ringel/README.md
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions matthew_ringel/gulpfile.js
Original file line number Diff line number Diff line change
@@ -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']);
2 changes: 2 additions & 0 deletions matthew_ringel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require(__dirname + '/lib/requesthandler');
require(__dirname + '/lib/server.js');
42 changes: 42 additions & 0 deletions matthew_ringel/lib/requesthandler.js
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 31 additions & 0 deletions matthew_ringel/lib/server.js
Original file line number Diff line number Diff line change
@@ -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');
});
34 changes: 34 additions & 0 deletions matthew_ringel/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
26 changes: 26 additions & 0 deletions matthew_ringel/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vanilla HTTP Server of Awesome</title>
</head>
<body>
<h1>Routes</h1>
<ul>
<li>/time</li>
<ul>
<li>returns the system time of the server in GMT</li>
</ul>
<li>/greet/[name]</li>
<ul>
<li>returns a greeting to [name]</li>
</ul>
<li>POST to /greet</li>
<ul>
<li>Expects a POST request to /greet sending JSON formatted as
{name: [name]} where [name] is a string</li>
<li>returns a greeting to [name]</li>
</ul>
</ul>
</body>
</html>
41 changes: 41 additions & 0 deletions matthew_ringel/test/server_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var chai = require('chai');
var chaihttp = require('chai-http');
chai.use(chaihttp);
var expect = chai.expect;
var fs = require('fs');
require(__dirname + '/../index');

describe('our server', function() {

it('should respond to a GET request to /time', function(done) {
chai.request('localhost:3000')
.get('/time')
.end(function(error, response) {
expect(error).to.eql(null);
expect(response).to.have.status(200);
done();
});
});

it('should respond to a GET request to /greet/[name]', function(done) {
chai.request('localhost:3000')
.get('/greet/name')
.end(function(error, response) {
expect(error).to.eql(null);
expect(response).to.have.status(200);
done();
});
});

it('should respond to a POST request to /greet with JSON data', function(done) {
chai.request('localhost:3000')
.post('/greet')
.send({"name": "bob"})
.end(function(error, response) {
expect(error).to.eql(null);
expect(response).to.have.status(200);
done();
});
});

});