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
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

2 changes: 2 additions & 0 deletions michael_tse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
data
12 changes: 7 additions & 5 deletions README.md → michael_tse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ Assignment Description
--------------------------
For this assignment, write an http server that will act as a simple data store.

It should respond to GET/POST requests for a single route and
the data coming in from a post request should be saved to a json file in
It should respond to GET/POST requests for a single route and
the data coming in from a post request should be saved to a json file in
a data folder in your repository. DO NOT commit your data folder to git. For example
if a request is sent to "/note_one" with a body of {noteBody: 'hello world'}
the json data in the body should be stored in it's own json file `data/note_one.json`.
if a request is sent to "/note_one" with a body of {noteBody: 'hello world'}
the json data in the body should be stored in it's own json file `data/note_one.json`.
A get request to the same route should return the data contained in the json file.

Rubric:

* Handles REST requests: 3pts
* JSON storage: 3pts
* JSON storage: 3pts
* Tests: 2pts
* Project Organization and Development Files: 2pts

Help from Emily
33 changes: 33 additions & 0 deletions michael_tse/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var mocha = require('gulp-mocha');
var appFiles = ['server.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,
beforeEach: true,
after: true
}
}))
.pipe(jshint.reporter('default'));
});

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

gulp.task('mocha', ['jshint'], function(){
return gulp.src('./test/test.js', {read:false})
.pipe(mocha({reporter:'spec'}));
});

gulp.task('jshint', ['jshint:test', 'jshint:app']);
gulp.task('default', ['jshint', 'mocha']);
21 changes: 21 additions & 0 deletions michael_tse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "http_with_persistence",
"version": "0.1.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "MIT",
"devDependencies": {
"chai": "^3.4.1",
"chai-http": "^1.0.0",
"gulp-jshint": "^1.12.0",
"gulp-mocha": "^2.1.3"
},
"dependencies": {
"express": "^4.13.3"
}
}
39 changes: 39 additions & 0 deletions michael_tse/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var express = require('express');
var app = express();
var http = require('http');
var fs = require('fs');

//converts incomming requests into string for future use
var processData = function(req, res, next) {
console.log('process data');
var data = '';
req.on('data', function(reqData) {
data = data + reqData.toString();
});
req.on('end', function(endData) {
req.body = data;
next();
});
};

app.use(processData);
Copy link
Contributor

Choose a reason for hiding this comment

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

is this necessary for this assignment?


//takes in a post request and stores newly made json file in data directory.
app.post('/:name', function(req, res) {
fs.writeFileSync(__dirname + '/data/' + req.params.name + '.json', req.body);
res.send(req.body);
//console log confirms to server file was posted
console.log('posted new file: ' + req.params.name + '.json');
});

// A get request to the same route should return the data contained in the json file.
app.get('/:name', function(req, res) {
fs.readFile(__dirname + '/data/' + req.params.name + '.json', function(err, data) {
if(err) throw err;
res.send(data.toString());
});
});

app.listen(3000, function() {
console.log('server up!!');
});
31 changes: 31 additions & 0 deletions michael_tse/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var chai = require('chai');
var chaihttp = require('chai-http');
chai.use(chaihttp);
var expect = chai.expect;
var fs = require('fs');
require(__dirname + '/../server');

describe('The server file', function() {
it('should respond to a post request', function(done) {
chai.request('localhost:3000')
.post('/note_one')
.send('{noteBody: \'hello world\'}')
.end(function(err, res) {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('{noteBody: \'hello world\'}');
done();
});
});
it('should be able to recieve and send json file to screen', function(done) {
chai.request('localhost:3000')
//assumes there is a sample.json file in the data directory with {"msg": "hello Felix"}
.get('/sample')
.end(function(err, res) {
expect(err).to.eql(null);
expect(res).to.have.status(200);
expect(res.text).to.eql('{"msg": \"hello Felix\"}\n');
done();
});
});
});