Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
**/*.sw?
**/node_modules
data/

Empty file added gulpfile.js
Empty file.
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
24 changes: 24 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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);
});
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});