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/README.md b/will_kitchell/README.md similarity index 100% rename from README.md rename to will_kitchell/README.md diff --git a/will_kitchell/package.json b/will_kitchell/package.json new file mode 100644 index 0000000..3d24b41 --- /dev/null +++ b/will_kitchell/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/will_kitchell/server.js b/will_kitchell/server.js new file mode 100644 index 0000000..82a6a79 --- /dev/null +++ b/will_kitchell/server.js @@ -0,0 +1,38 @@ +'use strict'; + +var fs = require('fs'); +var express = require('express'); +var app = express(); + +app.set('port', (process.env.PORT || 5000)); + +var handleData = function(req, res, next) { + var data = ''; + + 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('/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(app.get('port'), function() { + console.log('Server listening at ' + app.get('port') + '.'); +}); diff --git a/will_kitchell/test/test.js b/will_kitchell/test/test.js new file mode 100644 index 0000000..a0328be --- /dev/null +++ b/will_kitchell/test/test.js @@ -0,0 +1,39 @@ +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')); + }); +}); + +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(); + }); + }); +});