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 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 new file mode 100644 index 0000000..abd6d36 --- /dev/null +++ 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/index.js b/jenny_pollack/index.js new file mode 100644 index 0000000..8998f48 --- /dev/null +++ b/jenny_pollack/index.js @@ -0,0 +1,34 @@ +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 new file mode 100644 index 0000000..31f5a6d --- /dev/null +++ b/jenny_pollack/lib/server.js @@ -0,0 +1,33 @@ +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.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('/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 diff --git a/jenny_pollack/package.json b/jenny_pollack/package.json new file mode 100644 index 0000000..ff51ae4 --- /dev/null +++ b/jenny_pollack/package.json @@ -0,0 +1,25 @@ +{ + "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", + "devDependencies": { + "chai": "^3.4.1", + "chai-http": "^1.0.0", + "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..fc5c9c0 --- /dev/null +++ b/jenny_pollack/test/test.js @@ -0,0 +1,26 @@ +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(); + }); + }); +}); + +