diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 7ddf381..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -**/*.sw? -**/node_modules 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 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 new file mode 100644 index 0000000..2716f74 --- /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']; +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..c81de95 --- /dev/null +++ b/michael_tse/package.json @@ -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" + } +} diff --git a/michael_tse/server.js b/michael_tse/server.js new file mode 100644 index 0000000..c7fb2fa --- /dev/null +++ b/michael_tse/server.js @@ -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); + +//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!!'); +}); diff --git a/michael_tse/test/test.js b/michael_tse/test/test.js new file mode 100644 index 0000000..260e5fe --- /dev/null +++ b/michael_tse/test/test.js @@ -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(); + }); + }); +});