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 rick_patci/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
30 changes: 30 additions & 0 deletions rick_patci/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var chaihttp = require('chai-http');
var testFiles = ['test/**/*.js'];
var appFiles = ['index.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
}))
.pipe(jshint.reporter('default'));
})

gulp.task('jshint', ['jshint:test', 'jshint:app']);
gulp.task('default', ['jshint']);
Empty file added rick_patci/index.js
Empty file.
12 changes: 12 additions & 0 deletions rick_patci/lib/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<link rel = 'stylesheet' href='/style.css'><link>
</head>
<body>
<h3>Here are your options:</h3>
<p>http://localhost:3000/time</p>
<p>http://localhost:3000/greet</p>
<p>http://localhost:3000/greet/yournamehere</p>
</body>
</html>
3 changes: 3 additions & 0 deletions rick_patci/lib/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
background-color: red;
}
16 changes: 16 additions & 0 deletions rick_patci/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "rick_http_server",
"version": "0.1.0",
"description": "simple http server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rick Patci",
"license": "MIT",
"devDependencies": {
"chai-http": "^1.0.0",
"gulp": "^3.9.0",
"gulp-jshint": "^1.12.0"
}
}
48 changes: 48 additions & 0 deletions rick_patci/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var http = require('http');
var PORT = 3000;
var fs = require('fs');

var routesAvailable = {
root: '/',
currentTime: '/time',
greet: '/greet',
greetName: '/greet/yourname'
};

var server = http.createServer(function(req, res) {
var resData = {};

if(req.url === routesAvailable.root && req.method === 'GET') {
resData.status = 200;
resData.contentType = 'text/html';
resData.data = fs.readFileSync(__dirname + '/lib/index.html').toString();
}
if(req.url === '/style.css') {
res.writeHead(200, {
'Content-Type': 'text/css'
});
res.write(fs.readFileSync(__dirname + '/lib/style.css'));
return res.end();
}
if(req.url === routesAvailable.currentTime && req.method === 'GET') {
resData.status = 200;
resData.contentType = 'text/plain';
resData.data = new Date().getTime().toString();
}
if(req.url === routesAvailable.greet && req.method === 'POST') {

}
res.writeHead(resData.status || 400, {
'Content-Type' : resData.contentType || 'text/plain'
});
res.write(resData.data || 'not found');
res.end();
});

server.listen(PORT, function() {
console.log('Server listening on http://localhost:%s', PORT);
});

/* The server should respond to a request to /time that will send back
the current time of the server. */

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

describe('the server', function() {
before(function() {
this.indexFileString = fs.readFileSync(__dirname + '/../lib/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);
console.log(res);
done();
}.bind(this)); //uses a node-style callback. Angular uses promise-style callback
});
});