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 spencer_caldwell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules
File renamed without changes.
23 changes: 23 additions & 0 deletions spencer_caldwell/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var mocha = require('gulp-mocha');
var serverFiles = ['server.js'];
var testFiles = ['test/*.js'];

gulp.task('jshint:test', function() {
return gulp.src(testFiles)
.pipe(jshint.reporter('default'));
});

gulp.task('jshint:server', function() {
return gulp.src(serverFiles)
.pipe(jshint.reporter('default'));
});

gulp.task('mocha', function() {
return gulp.src('./test/*.js', {read: true})
.pipe(mocha({reporter: 'nyan'}));
});

gulp.task('jshint', ['jshint:test', 'jshint:server']);
gulp.task('default', ['jshint', 'mocha']);
10 changes: 10 additions & 0 deletions spencer_caldwell/lib/time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function() {
var date = Date.now();
// var hours = date.getHours();
// var minutes = '0' + date.getMinutes();
// var seconds = '0' + date.getSeconds();
// var formattedTime = hours + ':' + minutes.subtr(-2) + ':' + seconds.subtr(-2);
// formattedTimeString = formattedTime.toString();
var dateString = date.toString();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit commented-out code

return dateString;
};
23 changes: 23 additions & 0 deletions spencer_caldwell/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "spencer_caldwell",
"version": "1.0.0",
"description": "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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the readme, it should go in a readme, or another .txt document called "assignment" or something, not in the description field.

"main": "server.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Own your work! Fill out the author field :)

"license": "ISC",
"devDependencies": {
"chai": "^3.4.0",
"chai-http": "^1.0.0",
"gulp": "^3.9.0",
"gulp-jshint": "^2.0.0",
"gulp-mocha": "^2.2.0",
"jshint": "^2.8.0"
}
}
12 changes: 12 additions & 0 deletions spencer_caldwell/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Super Awesome Site</title>
</head>
<body>
<p>If you go to /greet/(some name), the server will print out hello and the name</p>
<p>If you go to /time, the server will send you its time in Unix time in milliseconds</p>
<p>If you submit a POST request to /greet, the server will take a name in JSON format</p>
</body>
</html>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent instructions page.

Empty file.
Empty file.
80 changes: 80 additions & 0 deletions spencer_caldwell/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var http = require('http');
var fs = require('fs');
var time = require(__dirname + '/lib/time');

var server = http.createServer(function(req, res) {
var resData = {};
var reqHeader = req.url;
var reqHeadStr = reqHeader.slice(7);

if (req.url === '/' && req.method === 'GET') {
resData.status = 200;
resData.contentType = 'text/html';
resData.data = fs.readFileSync(__dirname + '/public/index.html').toString();
res.writeHead(resData.status || 404, {
'Content-Type': resData.contentType || 'text/plain'});
res.write(resData.data || 'not found');
res.end();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing is a bit off here

}

else if (req.url === '/time' && req.method === 'GET') {
resData.status = 200;
resData.contentType = 'text/html';
resData.data = time();
res.writeHead(resData.status || 404, {
'Content-Type': resData.contentType || 'text/plain'});
res.write(resData.data || 'not found');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing again

res.end();
}

else if (req.url === '/greet/' + reqHeadStr && req.method === 'GET') {
resData.status = 200;
resData.contentType = 'text/html';
resData.data = 'Hello ' + reqHeadStr + '!';
res.writeHead(resData.status || 404, {
'Content-Type': resData.contentType || 'text/plain'});
res.write(resData.data || 'not found');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing again

res.end();
}

//code in question


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whitespace is not needed


else if (req.url === '/greet' && req.method === 'POST') {
var parse = '';
req.on('data', function(data) {
parse = JSON.parse(data);
});
req.on('end', function() {
resData.status = 200;
resData.contentType = 'text/html';
console.log(parse.name);
resData.data = parse.name;
res.writeHead(resData.status || 404, {
'Content-Type': resData.contentType || 'text/plain'});
res.write(resData.data || 'not found');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing again

res.end();

});
}

else {
res.writeHead(404, {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabbing again

'Content-Type': 'text/plain'
});
res.write('page not found');
res.end();
}

//end of code in question


});

server.listen(3000, function() {
console.log('server up');
})

debugger;
46 changes: 46 additions & 0 deletions spencer_caldwell/test/server_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var chai = require('chai');
var chaihttp = require('chai-http');
chai.use(chaihttp);
var expect = chai.expect;
var fs = require('fs');
require(__dirname + '/../server');

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));
});

it('should respond to /time by sending back the current time', function(done) {
chai.request('localhost:3000')
.get('/time')
.end(function(err, res) {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.be.above(1451540386398);
done();
});
});

it('should respond to /greet/* with the name you send it', function(done) {
chai.request('localhost:3000')
.get('/greet/test')
.end(function(err, res) {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('Hello test!');
done();
});
});

});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good styling here!