From cb6ccca04cdeb8b094b4c4f8d9edc9c77327f2c5 Mon Sep 17 00:00:00 2001 From: Will Kitchell Date: Tue, 10 Nov 2015 12:28:25 -0800 Subject: [PATCH 1/4] first commit --- data/data.json | 1 + package.json | 29 +++++++++++++++++++++++++++++ server.js | 17 +++++++++++++++++ test/test.js | 14 ++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 data/data.json create mode 100644 package.json create mode 100644 server.js create mode 100644 test/test.js diff --git a/data/data.json b/data/data.json new file mode 100644 index 0000000..3f3571f --- /dev/null +++ b/data/data.json @@ -0,0 +1 @@ +{"hello":"world"} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3d24b41 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ + { + "name": "http_with_persistence", + "version": "1.0.0", + "description": "Describe deeznutz", + "main": "server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/picklekitch/http_with_persistence.git" + }, + "author": "Will Kitchell (william.kitchell@gmail.com)", + "license": "ISC", + "bugs": { + "url": "https://github.com/picklekitch/http_with_persistence/issues" + }, + "homepage": "https://github.com/picklekitch/http_with_persistence#readme", + "devDependencies": { + "body-parser": "^1.14.1", + "chai": "^3.4.1", + "chai-http": "^1.0.0", + "express": "^4.13.3", + "mocha": "^2.3.3" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..cec56b1 --- /dev/null +++ b/server.js @@ -0,0 +1,17 @@ +var http = require('http'); +var express = require('express'); +var app = express(); +var fs = require('fs'); +var bodyParser = require('body-parser'); + +app.use(bodyParser.json()); + +app.get('/', function (req, res){ + res.json(JSON.parse(fs.readFileSync('Hello cruel world'))); +}); + +app.post('/', function (req, res){ + fs.writeFileSync('./data/data.json', JSON.stringify(req.body)); +}); + +app.listen(3000); diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..d6a853d --- /dev/null +++ b/test/test.js @@ -0,0 +1,14 @@ +var fs = require('fs'); +var chai = require('chai'); +var expect = chai.exepct; +var chaiHttp = require('chai-http'); +var express = require('express'); +chai.use(chaiHttp); + +var server = require(__dirname + '/../server.js'); + +describe('The server.js file', function (){ + it('should write a json file in the /data folder', function(done){ + expect(fs.readFileSync('./data/data.json')); + }); +}); From 9c7f430f02318909b6d7a74cc4bdcbedd7263525 Mon Sep 17 00:00:00 2001 From: Will Kitchell Date: Tue, 10 Nov 2015 12:31:47 -0800 Subject: [PATCH 2/4] removed the data folder and data.json file --- .gitignore | 1 + data/data.json | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 data/data.json 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/data/data.json b/data/data.json deleted file mode 100644 index 3f3571f..0000000 --- a/data/data.json +++ /dev/null @@ -1 +0,0 @@ -{"hello":"world"} \ No newline at end of file From d39e5cafc7e25ba7565c6bffb5f0c9d267c70a2a Mon Sep 17 00:00:00 2001 From: Will Kitchell Date: Mon, 21 Dec 2015 00:39:17 -0800 Subject: [PATCH 3/4] updated server and tests --- server.js | 39 ++++++++++++++++++++++++++++++--------- test/test.js | 25 +++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index cec56b1..82a6a79 100644 --- a/server.js +++ b/server.js @@ -1,17 +1,38 @@ -var http = require('http'); +'use strict'; + +var fs = require('fs'); var express = require('express'); var app = express(); -var fs = require('fs'); -var bodyParser = require('body-parser'); -app.use(bodyParser.json()); +app.set('port', (process.env.PORT || 5000)); + +var handleData = function(req, res, next) { + var data = ''; -app.get('/', function (req, res){ - res.json(JSON.parse(fs.readFileSync('Hello cruel world'))); + req.on('data', function(reqData) { + data = data + reqData.toString(); + }); + + req.on('end', function(endData) { + req.body = data; + next(); + }); +}; + +app.get('/data/:id', function(req, res) { + fs.readFile(__dirname + '/data/' + req.params.id + '.json', function(err, data) { + res.send(data.toString()); + }); }); -app.post('/', function (req, res){ - fs.writeFileSync('./data/data.json', JSON.stringify(req.body)); +app.post('/data/:id', handleData, function(req, res) { + fs.appendFile(__dirname + '/data/' + req.params.id + '.json', req.body, function(err) { + if (err) throw err; + + res.send('file created'); + }); }); -app.listen(3000); +app.listen(app.get('port'), function() { + console.log('Server listening at ' + app.get('port') + '.'); +}); diff --git a/test/test.js b/test/test.js index d6a853d..a0328be 100644 --- a/test/test.js +++ b/test/test.js @@ -12,3 +12,28 @@ describe('The server.js file', function (){ expect(fs.readFileSync('./data/data.json')); }); }); + +describe('post req', function() { + it('should respond to file creation', function(done) { + chai.request('http://localhost:5000') + .post('/data/test') + .send({'hello':'world'}) + .end(function(err, res) { + expect(res.text).to.eql('file created'); + done(); + }); + }); +}); + +describe('get req', function() { + it('should read test file', function(done) { + chai.request('http://localhost:5000') + .get('/data/testfile') + .end(function(err, res) { + expect(err).to.equal(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('{"hello":"world"}\n'); + done(); + }); + }); +}); From 50c957c06fb28ee1a0744550391a6ae8c41c64f4 Mon Sep 17 00:00:00 2001 From: Will Kitchell Date: Mon, 21 Dec 2015 00:46:35 -0800 Subject: [PATCH 4/4] put it all into a will_kitchell directory --- README.md => will_kitchell/README.md | 0 package.json => will_kitchell/package.json | 0 server.js => will_kitchell/server.js | 0 {test => will_kitchell/test}/test.js | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename README.md => will_kitchell/README.md (100%) rename package.json => will_kitchell/package.json (100%) rename server.js => will_kitchell/server.js (100%) rename {test => will_kitchell/test}/test.js (100%) diff --git a/README.md b/will_kitchell/README.md similarity index 100% rename from README.md rename to will_kitchell/README.md diff --git a/package.json b/will_kitchell/package.json similarity index 100% rename from package.json rename to will_kitchell/package.json diff --git a/server.js b/will_kitchell/server.js similarity index 100% rename from server.js rename to will_kitchell/server.js diff --git a/test/test.js b/will_kitchell/test/test.js similarity index 100% rename from test/test.js rename to will_kitchell/test/test.js