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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/*.sw?
**/node_modules
**/data
File renamed without changes.
29 changes: 29 additions & 0 deletions will_kitchell/package.json
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Body-parser should be a dependency, but you didn't use it, so it should be removed from this list.

"chai": "^3.4.1",
"chai-http": "^1.0.0",
"express": "^4.13.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Express is a dependency, not a devDependency.

"mocha": "^2.3.3"
}
}
38 changes: 38 additions & 0 deletions will_kitchell/server.js
Original file line number Diff line number Diff line change
@@ -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();
});
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a small project, but ideally your handleData function would be modularized.


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') + '.');
});
39 changes: 39 additions & 0 deletions will_kitchell/test/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});