diff --git a/.gitignore b/.gitignore index 7ddf381..4334c55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ **/*.sw? **/node_modules +data/ + diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..634f4d8 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "http_with_persistence", + "version": "0.1.0", + "description": "GET/POST JSON", + "main": "index.js", + "scripts": { + "test": "./node_modules/mocha/bin/mocha test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/patci/http_with_persistence.git" + }, + "author": "rpatci@gmail.com", + "license": "MIT", + "bugs": { + "url": "https://github.com/patci/http_with_persistence/issues" + }, + "homepage": "https://github.com/patci/http_with_persistence#readme", + "dependencies": { + "body-parser": "^1.14.1", + "express": "^4.13.3" + }, + "devDependencies": { + "chai": "^3.4.1", + "chai-http": "^1.0.0", + "mocha": "^2.3.3" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..37fca89 --- /dev/null +++ b/server.js @@ -0,0 +1,24 @@ +var express = require('express'); +var app = express(); +var port = process.env.PORT || 3000; +var fs = require('fs'); + +app.post('/note_one', function(req, res, next) { + req.on('data', function(data) { + var currentData = data.toString(); + fs.writeFile(__dirname + '/data/note_one.json', currentData, function() { + }); + res.send('file saved!'); + }); + }); + +app.get('/note_one', function(req, res) { + fs.readFile(__dirname + '/data/note_one.json', function(err, data) { + if (err) return console.log(err); + res.send(data.toString()); + }); +}); + +app.listen(port, function() { + console.log('server up on port ' + port); +}); diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..94d8032 --- /dev/null +++ b/test/test.js @@ -0,0 +1,27 @@ +var chai = require('chai'); +var chaihttp = require('chai-http'); +chai.use(chaihttp); +var expect = chai.expect; +var fs = require('fs'); +require(__dirname + '/../server'); + +describe('this little server of mine', function() { + it('should handle a POST request', function(done) { + chai.request('localhost:3000') + .post('/note_one') + .send({'hey': 'itsMeJSON'}) + .end(function(err, res) { + expect(err).to.eql(null); + expect(res.status).to.eql(200); + done(); + }); + }); + it('should handle a GET request', function() { + chai.request('localhost:3000') + .get('/note_one') + .end(function(err, res) { + expect(err).to.eql(null); + expect(res.status).to.eql(200); + }); + }); +});