From 95f0a97e473774c23fceb936182ed74b53792511 Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Tue, 10 Nov 2015 11:03:14 -0800 Subject: [PATCH 1/7] add structure --- jenny_pollack/gulpfile.js | 0 jenny_pollack/index.js | 0 jenny_pollack/package.json | 14 ++++++++++++++ 3 files changed, 14 insertions(+) create mode 100644 jenny_pollack/gulpfile.js create mode 100644 jenny_pollack/index.js create mode 100644 jenny_pollack/package.json diff --git a/jenny_pollack/gulpfile.js b/jenny_pollack/gulpfile.js new file mode 100644 index 0000000..e69de29 diff --git a/jenny_pollack/index.js b/jenny_pollack/index.js new file mode 100644 index 0000000..e69de29 diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json new file mode 100644 index 0000000..49d7fbb --- /dev/null +++ b/jenny_pollack/package.json @@ -0,0 +1,14 @@ +{ + "name": "http_persistence", + "version": "1.0.0", + "description": "an http server that will act as a simple data store", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "test" + }, + "author": "Jenny Pollack", + "license": "MIT" +} From 6a6558f992eef80407a607fb261154efd076378f Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Tue, 10 Nov 2015 11:16:25 -0800 Subject: [PATCH 2/7] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 7ddf381..74c0a48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **/*.sw? **/node_modules +**/data \ No newline at end of file From 40179d3a4479116f9869369fe86bcd275f1791df Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Tue, 10 Nov 2015 12:15:53 -0800 Subject: [PATCH 3/7] add license, packages --- jenny_pollack/LICENSE | 25 +++++++++++++++++++++ jenny_pollack/gulpfile.js | 43 +++++++++++++++++++++++++++++++++++++ jenny_pollack/lib/server.js | 15 +++++++++++++ jenny_pollack/package.json | 12 ++++++++++- jenny_pollack/test/test.js | 1 + 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 jenny_pollack/LICENSE create mode 100644 jenny_pollack/lib/server.js create mode 100644 jenny_pollack/test/test.js diff --git a/jenny_pollack/LICENSE b/jenny_pollack/LICENSE new file mode 100644 index 0000000..9d5ddd4 --- /dev/null +++ b/jenny_pollack/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) <2015> + + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + + + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + + + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/jenny_pollack/gulpfile.js b/jenny_pollack/gulpfile.js index e69de29..abd6d36 100644 --- a/jenny_pollack/gulpfile.js +++ b/jenny_pollack/gulpfile.js @@ -0,0 +1,43 @@ +var gulp = require('gulp'); +var jshint = require('gulp-jshint'); +var mocha = require('gulp-mocha'); +var appFiles = ['index.js', 'lib/**/*.js', 'bin/**/*.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, + 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('mocha', function(){ + return gulp.src(testFiles) + .pipe(jshint({ + node: true, + globals: { + describe: true, + it: true + } + })) + .pipe(mocha({reporter: 'spec'})); +}); + + +gulp.task('jshint', ['jshint:test', 'jshint:app']); +gulp.task('default', ['jshint', 'mocha']); \ No newline at end of file diff --git a/jenny_pollack/lib/server.js b/jenny_pollack/lib/server.js new file mode 100644 index 0000000..8047da2 --- /dev/null +++ b/jenny_pollack/lib/server.js @@ -0,0 +1,15 @@ +var app = require('express')(); +var bodyParser = require('body-parser'); +var fs = require('fs'); + +app.get('/greet/:name', function(req, res) { + res.json({msg: 'hell0 ' + req.params.name}); +}); + +app.post('/greet', function(req, res){ + +}); + +app.listen(3000, function() { + console.log('server up'); +}); \ No newline at end of file diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json index 49d7fbb..7f6c158 100644 --- a/jenny_pollack/package.json +++ b/jenny_pollack/package.json @@ -10,5 +10,15 @@ "test": "test" }, "author": "Jenny Pollack", - "license": "MIT" + "license": "MIT", + "devDependencies": { + "chai": "^3.4.1", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3", + "mocha": "^2.3.3" + }, + "dependencies": { + "body-parser": "^1.14.1", + "express": "^4.13.3" + } } diff --git a/jenny_pollack/test/test.js b/jenny_pollack/test/test.js new file mode 100644 index 0000000..7beeccf --- /dev/null +++ b/jenny_pollack/test/test.js @@ -0,0 +1 @@ +//what do i want my tests to do? \ No newline at end of file From bc6956cebf9c0ef0596a315eb1dd6b5234b93229 Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Tue, 10 Nov 2015 21:34:04 -0800 Subject: [PATCH 4/7] i think it works --- jenny_pollack/index.js | 1 + jenny_pollack/lib/server.js | 14 +++++++++----- jenny_pollack/package.json | 1 + jenny_pollack/test/test.js | 28 +++++++++++++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/jenny_pollack/index.js b/jenny_pollack/index.js index e69de29..7b4a69b 100644 --- a/jenny_pollack/index.js +++ b/jenny_pollack/index.js @@ -0,0 +1 @@ +require(__dirname + '/lib/server.js'); \ No newline at end of file diff --git a/jenny_pollack/lib/server.js b/jenny_pollack/lib/server.js index 8047da2..5e17bf2 100644 --- a/jenny_pollack/lib/server.js +++ b/jenny_pollack/lib/server.js @@ -1,15 +1,19 @@ var app = require('express')(); -var bodyParser = require('body-parser'); var fs = require('fs'); -app.get('/greet/:name', function(req, res) { - res.json({msg: 'hell0 ' + req.params.name}); +app.get('/greet', function(req, res) { + fs.readFile(__dirname + '/../data/greet.json', function(err, data){ + res.send(JSON.parse(data.toString())); + }); + }); app.post('/greet', function(req, res){ - + var info = fs.createWriteStream(__dirname + '/../data/greet.json'); + req.pipe(info); + res.send('written'); }); app.listen(3000, function() { console.log('server up'); -}); \ No newline at end of file +}); diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json index 7f6c158..ff51ae4 100644 --- a/jenny_pollack/package.json +++ b/jenny_pollack/package.json @@ -13,6 +13,7 @@ "license": "MIT", "devDependencies": { "chai": "^3.4.1", + "chai-http": "^1.0.0", "gulp-jshint": "^1.12.0", "gulp-mocha": "^2.1.3", "mocha": "^2.3.3" diff --git a/jenny_pollack/test/test.js b/jenny_pollack/test/test.js index 7beeccf..fecc79e 100644 --- a/jenny_pollack/test/test.js +++ b/jenny_pollack/test/test.js @@ -1 +1,27 @@ -//what do i want my tests to do? \ No newline at end of file +//what do i want my tests to do? +var fs = require('fs'); +var chai = require('chai'); +var chaiHttp = require('chai-http'); +var express = require('express'); +var expect = chai.expect; + +chai.use(chaiHttp); + + +require(__dirname + '/../lib/server'); + +describe('The server.js file', function (){ + it('should respond to a post request by overwriting a file', function(done){ + chai.request('localhost:3000') + .post('/greet') + .send({test: 'jello'}) + .end(function(err, res){ + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('written'); + done(); + }) + }); +}); + + From d03a8d26b34afb32cc85063d7d6ebfc85ba3caf9 Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Tue, 10 Nov 2015 21:35:26 -0800 Subject: [PATCH 5/7] fix format issue --- jenny_pollack/test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenny_pollack/test/test.js b/jenny_pollack/test/test.js index fecc79e..8f3f322 100644 --- a/jenny_pollack/test/test.js +++ b/jenny_pollack/test/test.js @@ -20,7 +20,7 @@ describe('The server.js file', function (){ expect(res).to.have.status(200); expect(res.text).to.eql('written'); done(); - }) + }); }); }); From b016feb3d654c2646d1b149bc6879f9c30704153 Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Sun, 29 Nov 2015 13:34:51 -0800 Subject: [PATCH 6/7] working --- jenny_pollack/index.js | 35 +++++++++++++++++++++++++++++++++- jenny_pollack/lib/server.js | 38 +++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/jenny_pollack/index.js b/jenny_pollack/index.js index 7b4a69b..8998f48 100644 --- a/jenny_pollack/index.js +++ b/jenny_pollack/index.js @@ -1 +1,34 @@ -require(__dirname + '/lib/server.js'); \ No newline at end of file +require(__dirname + '/lib/server.js'); +// var express = require('express'); +// var app = express(); +// var fs = require('fs'); + +// var processData = function(req, res, next) { +// var data = ''; +// req.on('data', function(newData) { +// data += newData.toString(); +// }); +// req.on('end', function() { +// req.body = data; +// next(); +// }); +// }; + +// app.get('/data/:name', function(req, res) { +// fs.readFile(__dirname + '/data/' + req.params.name + '.json', function(err, data) { +// res.send(data); +// }); +// }); + +// app.use(processData); + +// app.post('/data/:name', function(req, res) { +// fs.writeFile(__dirname + '/data/' + req.params.name + '.json', req.body, function(err){ +// if (err) console.log(err); +// res.send(req.params.name + '.json has been written.'); +// }); +// }); + +// app.listen(3000, function() { +// console.log('server up at port 3000'); +// }); \ No newline at end of file diff --git a/jenny_pollack/lib/server.js b/jenny_pollack/lib/server.js index 5e17bf2..31f5a6d 100644 --- a/jenny_pollack/lib/server.js +++ b/jenny_pollack/lib/server.js @@ -1,19 +1,33 @@ -var app = require('express')(); -var fs = require('fs'); +var express = require('express'); +var app = express(); +var fs = require('fs'); -app.get('/greet', function(req, res) { - fs.readFile(__dirname + '/../data/greet.json', function(err, data){ - res.send(JSON.parse(data.toString())); - }); +var processData = function(req, res, next) { + var data = ''; + req.on('data', function(newData) { + data += newData.toString(); + }); + req.on('end', function() { + req.body = data; + next(); + }); +}; +app.use(processData); + +app.get('/data/:name', function(req, res) { + fs.readFile(__dirname + '/../data/' + req.params.name + '.json', function(err, data) { + res.send(data.toString()); + }); }); -app.post('/greet', function(req, res){ - var info = fs.createWriteStream(__dirname + '/../data/greet.json'); - req.pipe(info); - res.send('written'); -}); +app.post('/data/:name', function(req, res) { + fs.writeFile(__dirname + '/../data/' + req.params.name + '.json', req.body, function(err){ + if (err) console.log(err); + res.send("written"); + }); +}); app.listen(3000, function() { console.log('server up'); -}); +}); \ No newline at end of file From 8f41946d4016ce25c1cfa4c41fab66d2ab2de327 Mon Sep 17 00:00:00 2001 From: Jenny Pollack Date: Sun, 29 Nov 2015 13:46:10 -0800 Subject: [PATCH 7/7] add edited test --- jenny_pollack/test/test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/jenny_pollack/test/test.js b/jenny_pollack/test/test.js index 8f3f322..fc5c9c0 100644 --- a/jenny_pollack/test/test.js +++ b/jenny_pollack/test/test.js @@ -1,4 +1,3 @@ -//what do i want my tests to do? var fs = require('fs'); var chai = require('chai'); var chaiHttp = require('chai-http');