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
1 change: 1 addition & 0 deletions jenny_pollack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
49 changes: 49 additions & 0 deletions jenny_pollack/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']);
18 changes: 18 additions & 0 deletions jenny_pollack/index.js
Original file line number Diff line number Diff line change
@@ -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)
95 changes: 95 additions & 0 deletions jenny_pollack/lib/server.js
Original file line number Diff line number Diff line change
@@ -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');
});


26 changes: 26 additions & 0 deletions jenny_pollack/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
21 changes: 21 additions & 0 deletions jenny_pollack/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Super Awesome Site</title>
</head>
<body>
<h1>Wow, here is an h1, so h1, much tag</h1>
You should have an html page that describes the
routes implemented by the api available
<h2>Routes Available</h2>
<ul>
<li>You can go to /time and I will tell you the time on the server</li>
<li>You can go to /greet and I will say hello</li>
<li>You can go to /greet/name and I will say hello + name</li>
</ul>

</body>
</html>


57 changes: 57 additions & 0 deletions jenny_pollack/test/http_test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});