From 94e6b8d9f9245b50f9ab7be157dd83ba937ea420 Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:02:14 -0800 Subject: [PATCH 1/7] first commit --- robin_stringer/.gitignore | 1 + robin_stringer/README.md | 27 +++++++++++++++++++++ robin_stringer/app.js | 35 +++++++++++++++++++++++++++ robin_stringer/data/answers.js | 0 robin_stringer/gulpfile.js | 43 ++++++++++++++++++++++++++++++++++ robin_stringer/package.json | 22 +++++++++++++++++ robin_stringer/test/test.js | 20 ++++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 robin_stringer/.gitignore create mode 100644 robin_stringer/README.md create mode 100644 robin_stringer/app.js create mode 100644 robin_stringer/data/answers.js create mode 100644 robin_stringer/gulpfile.js create mode 100644 robin_stringer/package.json create mode 100644 robin_stringer/test/test.js diff --git a/robin_stringer/.gitignore b/robin_stringer/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/robin_stringer/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/robin_stringer/README.md b/robin_stringer/README.md new file mode 100644 index 0000000..a35695b --- /dev/null +++ b/robin_stringer/README.md @@ -0,0 +1,27 @@ +HTTP Server With Persistence +============================ +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 + +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 +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`. +A get request to the same route should return the data contained in the json file. + +Rubric: + + * Handles REST requests: 3pts + * JSON storage: 3pts + * Tests: 2pts + * Project Organization and Development Files: 2pts diff --git a/robin_stringer/app.js b/robin_stringer/app.js new file mode 100644 index 0000000..9689772 --- /dev/null +++ b/robin_stringer/app.js @@ -0,0 +1,35 @@ + +'use strict'; + +var express = require('express'), +var app = express(); +var fs = require('fs'); +var bodyParser = require('body-parser'); +var http = require('http'); + +//with thanks to Treehouse for tutorial on bodyParser +//(https://teamtreehouse.com/library/express-basics) +app.use(bodyParser.json()); + +app.get('/answers', function(req, res, next){ + fs.readFile(__dirname + '/../data/answers.json', function(err, data){ + if(err) throw err; + next(); + }) + res.send(answers.toString()); +}); + +app.post('/answers', function(req, res, next) { + req.on('data', function(data) { + var dataLogged = data.toString(); + fs.writeFile(__dirname + '/../data/answers.json', dataLogged, function() { + if(err) throw err; + next(); + }); + }); + res.send('POST request to the homepage'); +}); + +var server = app.listen(3000, function(){ + console.log("The server is running on port 3000!") +}); diff --git a/robin_stringer/data/answers.js b/robin_stringer/data/answers.js new file mode 100644 index 0000000..e69de29 diff --git a/robin_stringer/gulpfile.js b/robin_stringer/gulpfile.js new file mode 100644 index 0000000..6c15ffa --- /dev/null +++ b/robin_stringer/gulpfile.js @@ -0,0 +1,43 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['app.js', 'gulpfile.js']; +var testFiles = ['test/**/*.js']; + +gulp.task('mocha:test', function() { + return gulp.src(testFiles, appFiles) + .pipe(mocha({ + node: true, + globals: { + describe: true, + it: true, + before: true, + after: true + } + })) + .pipe(mocha({reporter: 'nyan'})); +}); + +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']); diff --git a/robin_stringer/package.json b/robin_stringer/package.json new file mode 100644 index 0000000..a6e933d --- /dev/null +++ b/robin_stringer/package.json @@ -0,0 +1,22 @@ +{ + "name": "robin_stringer", + "version": "0.1.0", + "description": "", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rastringer/http_with_persistence.git" + }, + "author": "ra1stringer@gmail.com", + "license": "MIT", + "bugs": { + "url": "https://github.com/rastringer/http_with_persistence/issues" + }, + "homepage": "https://github.com/rastringer/http_with_persistence#readme", + "devDependencies": { + "body-parser": "^1.14.1" + } +} diff --git a/robin_stringer/test/test.js b/robin_stringer/test/test.js new file mode 100644 index 0000000..d255e6f --- /dev/null +++ b/robin_stringer/test/test.js @@ -0,0 +1,20 @@ +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 express server', function() { + it('should respond to POST requests and return values', function(done) { + chai.request('localhost:3000') + .post('/answers') + .send(json) + .end function(err, res){ + expect(err).to.eql(null); + expect(res.status).to.eql(200); + expect(res.text).to.eql('Thank you for your request'); + done(); + }); +}); + From a8b990da8d7925fb436315f10da609f14cfda81f Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:16:06 -0800 Subject: [PATCH 2/7] adds tests --- robin_stringer/app.js | 6 +++--- robin_stringer/package.json | 7 +++++++ robin_stringer/test/test.js | 10 +++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/robin_stringer/app.js b/robin_stringer/app.js index 9689772..d185eb0 100644 --- a/robin_stringer/app.js +++ b/robin_stringer/app.js @@ -1,7 +1,7 @@ 'use strict'; -var express = require('express'), +var express = require('express'); var app = express(); var fs = require('fs'); var bodyParser = require('body-parser'); @@ -15,7 +15,7 @@ app.get('/answers', function(req, res, next){ fs.readFile(__dirname + '/../data/answers.json', function(err, data){ if(err) throw err; next(); - }) + }); res.send(answers.toString()); }); @@ -31,5 +31,5 @@ app.post('/answers', function(req, res, next) { }); var server = app.listen(3000, function(){ - console.log("The server is running on port 3000!") + console.log("The server is running on port 3000!"); }); diff --git a/robin_stringer/package.json b/robin_stringer/package.json index a6e933d..026e7f5 100644 --- a/robin_stringer/package.json +++ b/robin_stringer/package.json @@ -18,5 +18,12 @@ "homepage": "https://github.com/rastringer/http_with_persistence#readme", "devDependencies": { "body-parser": "^1.14.1" + "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/robin_stringer/test/test.js b/robin_stringer/test/test.js index d255e6f..88ea343 100644 --- a/robin_stringer/test/test.js +++ b/robin_stringer/test/test.js @@ -3,18 +3,18 @@ var chaihttp = require('chai-http'); chai.use(chaihttp); var expect = chai.expect; var fs = require('fs'); -require(__dirname + '/../server'); +require(__dirname + '/../app'); describe('the express server', function() { it('should respond to POST requests and return values', function(done) { chai.request('localhost:3000') - .post('/answers') - .send(json) - .end function(err, res){ + .post('/note_one') + .send('{noteBody: \'hello world\'}') + .end(function(err, res) { expect(err).to.eql(null); expect(res.status).to.eql(200); expect(res.text).to.eql('Thank you for your request'); done(); }); }); - +}); From 95466219db2a00a74e976b23c07cfa508e5c4343 Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:19:23 -0800 Subject: [PATCH 3/7] altering .gitignore --- robin_stringer/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/robin_stringer/.gitignore b/robin_stringer/.gitignore index 3c3629e..15de390 100644 --- a/robin_stringer/.gitignore +++ b/robin_stringer/.gitignore @@ -1 +1,2 @@ node_modules +data From a39c3fc804b2d420a527220f55d8707a4c03ef4f Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:20:05 -0800 Subject: [PATCH 4/7] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7ddf381..e1f4760 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/*.sw? **/node_modules +**/data From a57228d2f1cf9e91ab089a36f1a7d26d656074cc Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:22:35 -0800 Subject: [PATCH 5/7] updates tests --- robin_stringer/.gitignore | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 robin_stringer/.gitignore diff --git a/robin_stringer/.gitignore b/robin_stringer/.gitignore deleted file mode 100644 index 15de390..0000000 --- a/robin_stringer/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules -data From 4816b0bb4d0c06ad59da63c24dcae5b4979e1886 Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:27:46 -0800 Subject: [PATCH 6/7] gulpfile --- robin_stringer/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 robin_stringer/.gitignore diff --git a/robin_stringer/.gitignore b/robin_stringer/.gitignore new file mode 100644 index 0000000..aa15018 --- /dev/null +++ b/robin_stringer/.gitignore @@ -0,0 +1,2 @@ +node_modules +data \ No newline at end of file From 0ba43b1f645219fbea1b0b1d9dd00ee3d16fca0a Mon Sep 17 00:00:00 2001 From: rastringer Date: Tue, 10 Nov 2015 19:31:21 -0800 Subject: [PATCH 7/7] jshint --- robin_stringer/.DS_Store | Bin 0 -> 6148 bytes robin_stringer/data/answers.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 robin_stringer/.DS_Store delete mode 100644 robin_stringer/data/answers.js diff --git a/robin_stringer/.DS_Store b/robin_stringer/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0