From d26f41989adca00850f95ca34e4c885f0abb79bb Mon Sep 17 00:00:00 2001 From: Michael Tse Date: Tue, 10 Nov 2015 12:46:44 -0800 Subject: [PATCH 1/5] able to return json file in file to webpage others still in progress --- .gitignore | 1 + michael_tse/gulpfile.js | 33 ++++++++++++++++ michael_tse/package.json | 20 ++++++++++ michael_tse/server.js | 78 ++++++++++++++++++++++++++++++++++++++ michael_tse/tests/tests.js | 22 +++++++++++ 5 files changed, 154 insertions(+) create mode 100644 michael_tse/gulpfile.js create mode 100644 michael_tse/package.json create mode 100644 michael_tse/server.js create mode 100644 michael_tse/tests/tests.js diff --git a/.gitignore b/.gitignore index 7ddf381..e1f4760 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/*.sw? **/node_modules +**/data diff --git a/michael_tse/gulpfile.js b/michael_tse/gulpfile.js new file mode 100644 index 0000000..a8bc475 --- /dev/null +++ b/michael_tse/gulpfile.js @@ -0,0 +1,33 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['server.js', 'lib/**/*.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']); diff --git a/michael_tse/package.json b/michael_tse/package.json new file mode 100644 index 0000000..71d3a4e --- /dev/null +++ b/michael_tse/package.json @@ -0,0 +1,20 @@ +{ + "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", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3" + }, + "dependencies": { + "express": "^4.13.3" + } +} diff --git a/michael_tse/server.js b/michael_tse/server.js new file mode 100644 index 0000000..ab472f6 --- /dev/null +++ b/michael_tse/server.js @@ -0,0 +1,78 @@ +var express = require('express'); +var app = express(); +var http = require('http'); +var fs = require('fs'); + + + +// 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. + +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); + +app.post('/:name', function(req, res) { + console.log('post me!') + fs.writeFile(__dirname + '/data/' + req.params.name + '.json', function(err) { + if(err) throw err; + res.send(req.body); + console.log('sucess post!'); + }); + res.send('data recieved'); +}); + + + +// app.get('/:name', function(req, res, next) { +// // var data = {} +// var data = fs.readFileSync(__dirname + '/data/' + req.params.name + '.json') +// res.send(JSON.parse(data)); + +// // fs.readFile(__dirname + '/data/' + req.params.name + '.json', function(err, data) { +// // if(err) throw err; +// // data = ; +// // }); +// next(); +// }); + + + + + + + +// app.post('/:name', function(req, res) { +// console.log('store that data!'); +// res.send() +// }) + + +// 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. +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!!'); +}); diff --git a/michael_tse/tests/tests.js b/michael_tse/tests/tests.js new file mode 100644 index 0000000..60c5ee9 --- /dev/null +++ b/michael_tse/tests/tests.js @@ -0,0 +1,22 @@ +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() { +// before(function() { +// this.indexFileString = fs.readFileSync(__dirname + '/../data/sampleJSON').toString(); +// }); +// J + it('should be able to recieve and send json file to screen', function(done) { + chai.request('localhost:3000') + .get('/sample') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.be.json; + done(); + }.bind(this)); + }); +}); From 03e0ccc0f5fcc077a3327d396658e4bfd6ffe47f Mon Sep 17 00:00:00 2001 From: Michael Tse Date: Tue, 10 Nov 2015 17:22:53 -0800 Subject: [PATCH 2/5] post request works and can get json file to display on page --- michael_tse/server.js | 48 ++++---------------------------------- michael_tse/tests/tests.js | 13 ++++++++++- 2 files changed, 16 insertions(+), 45 deletions(-) diff --git a/michael_tse/server.js b/michael_tse/server.js index ab472f6..ecab6b9 100644 --- a/michael_tse/server.js +++ b/michael_tse/server.js @@ -3,12 +3,6 @@ var app = express(); var http = require('http'); var fs = require('fs'); - - -// 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. - var processData = function(req, res, next) { console.log('process data'); var data = ''; @@ -23,48 +17,14 @@ var processData = function(req, res, next) { app.use(processData); +//takes in a post request and stores newly made json file in data directory. app.post('/:name', function(req, res) { - console.log('post me!') - fs.writeFile(__dirname + '/data/' + req.params.name + '.json', function(err) { - if(err) throw err; + fs.writeFileSync(__dirname + '/data/' + req.params.name + '.json', req.body); res.send(req.body); - console.log('sucess post!'); - }); - res.send('data recieved'); + //console log confirms to server file was posted + console.log('posted new file: ' + req.params.name + '.json'); }); - - -// app.get('/:name', function(req, res, next) { -// // var data = {} -// var data = fs.readFileSync(__dirname + '/data/' + req.params.name + '.json') -// res.send(JSON.parse(data)); - -// // fs.readFile(__dirname + '/data/' + req.params.name + '.json', function(err, data) { -// // if(err) throw err; -// // data = ; -// // }); -// next(); -// }); - - - - - - - -// app.post('/:name', function(req, res) { -// console.log('store that data!'); -// res.send() -// }) - - -// 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. app.get('/:name', function(req, res) { fs.readFile(__dirname + '/data/' + req.params.name + '.json', function(err, data) { diff --git a/michael_tse/tests/tests.js b/michael_tse/tests/tests.js index 60c5ee9..eb5ef20 100644 --- a/michael_tse/tests/tests.js +++ b/michael_tse/tests/tests.js @@ -9,7 +9,17 @@ describe('The server file', function() { // before(function() { // this.indexFileString = fs.readFileSync(__dirname + '/../data/sampleJSON').toString(); // }); -// J + + it('should be able to recieve and send json file to screen', function(done) { + chai.request('localhost:3000') + .get('/note_one') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res).to.be.json; + done(); + }.bind(this)); + }); + it('should be able to recieve and send json file to screen', function(done) { chai.request('localhost:3000') .get('/sample') @@ -19,4 +29,5 @@ describe('The server file', function() { done(); }.bind(this)); }); + }); From 0520c22bdf45bf23924c512b7a0c241cb3e80b68 Mon Sep 17 00:00:00 2001 From: Michael Tse Date: Tue, 10 Nov 2015 18:17:57 -0800 Subject: [PATCH 3/5] added tests moved readme to inside folder package.json needed chai-http --- README.md => michael_tse/README.md | 12 ++++++----- michael_tse/gulpfile.js | 2 +- michael_tse/package.json | 1 + michael_tse/server.js | 1 + michael_tse/{tests/tests.js => test/test.js} | 22 +++++++++----------- 5 files changed, 20 insertions(+), 18 deletions(-) rename README.md => michael_tse/README.md (87%) rename michael_tse/{tests/tests.js => test/test.js} (55%) diff --git a/README.md b/michael_tse/README.md similarity index 87% rename from README.md rename to michael_tse/README.md index a35695b..ec17f53 100644 --- a/README.md +++ b/michael_tse/README.md @@ -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 diff --git a/michael_tse/gulpfile.js b/michael_tse/gulpfile.js index a8bc475..2716f74 100644 --- a/michael_tse/gulpfile.js +++ b/michael_tse/gulpfile.js @@ -1,7 +1,7 @@ var gulp = require('gulp'); var jshint = require('gulp-jshint'); var mocha = require('gulp-mocha'); -var appFiles = ['server.js', 'lib/**/*.js']; +var appFiles = ['server.js']; var testFiles = ['./test/**/*.js']; gulp.task('jshint:test', function(){ diff --git a/michael_tse/package.json b/michael_tse/package.json index 71d3a4e..c81de95 100644 --- a/michael_tse/package.json +++ b/michael_tse/package.json @@ -11,6 +11,7 @@ "license": "MIT", "devDependencies": { "chai": "^3.4.1", + "chai-http": "^1.0.0", "gulp-jshint": "^1.12.0", "gulp-mocha": "^2.1.3" }, diff --git a/michael_tse/server.js b/michael_tse/server.js index ecab6b9..c7fb2fa 100644 --- a/michael_tse/server.js +++ b/michael_tse/server.js @@ -3,6 +3,7 @@ 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 = ''; diff --git a/michael_tse/tests/tests.js b/michael_tse/test/test.js similarity index 55% rename from michael_tse/tests/tests.js rename to michael_tse/test/test.js index eb5ef20..260e5fe 100644 --- a/michael_tse/tests/tests.js +++ b/michael_tse/test/test.js @@ -6,28 +6,26 @@ var fs = require('fs'); require(__dirname + '/../server'); describe('The server file', function() { -// before(function() { -// this.indexFileString = fs.readFileSync(__dirname + '/../data/sampleJSON').toString(); -// }); - - it('should be able to recieve and send json file to screen', function(done) { + it('should respond to a post request', function(done) { chai.request('localhost:3000') - .get('/note_one') + .post('/note_one') + .send('{noteBody: \'hello world\'}') .end(function(err, res) { expect(err).to.eql(null); - expect(res).to.be.json; + expect(res).to.have.status(200); + expect(res.text).to.eql('{noteBody: \'hello world\'}'); done(); - }.bind(this)); + }); }); - 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.be.json; + expect(res).to.have.status(200); + expect(res.text).to.eql('{"msg": \"hello Felix\"}\n'); done(); - }.bind(this)); + }); }); - }); From 08d198c9205b4e19d143229e59618da674d6248c Mon Sep 17 00:00:00 2001 From: Michael Tse Date: Thu, 12 Nov 2015 11:54:29 -0800 Subject: [PATCH 4/5] changed git ignore --- michael_tse/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 michael_tse/.gitignore diff --git a/michael_tse/.gitignore b/michael_tse/.gitignore new file mode 100644 index 0000000..15de390 --- /dev/null +++ b/michael_tse/.gitignore @@ -0,0 +1,2 @@ +node_modules +data From 916ba411a12a418f3a2f5767cf9639e58216d632 Mon Sep 17 00:00:00 2001 From: Michael Tse Date: Thu, 12 Nov 2015 11:57:30 -0800 Subject: [PATCH 5/5] gitignore fixed --- .gitignore | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e1f4760..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -**/*.sw? -**/node_modules -**/data