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
Copy link
Contributor

Choose a reason for hiding this comment

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

I won't be able to merge because of changing this file.

Binary file added robin_stringer/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions robin_stringer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
data
Copy link
Contributor

Choose a reason for hiding this comment

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

good!

27 changes: 27 additions & 0 deletions robin_stringer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
HTTP Server With Persistence
============================
To complete this assignment:
* fork this repository (the sub module for this specific assignment)
* clone down your fork
* place all of your work in a folder that is your full name, use `_`s instead of spaces
* push back up to your fork
* create a pull request back to the original repo
* submit a link to the PR in canvas

Assignment Description
--------------------------
For this assignment, write an http server that will act as a simple data store.

It should respond to GET/POST requests for a single route and
the data coming in from a post request should be saved to a json file in
a data folder in your repository. DO NOT commit your data folder to git. For example
if a request is sent to "/note_one" with a body of {noteBody: 'hello world'}
the json data in the body should be stored in it's own json file `data/note_one.json`.
A get request to the same route should return the data contained in the json file.

Rubric:

* Handles REST requests: 3pts
* JSON storage: 3pts
* Tests: 2pts
* Project Organization and Development Files: 2pts
35 changes: 35 additions & 0 deletions robin_stringer/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

'use strict';

var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var http = require('http');

//with thanks to Treehouse for tutorial on bodyParser
//(https://teamtreehouse.com/library/express-basics)
app.use(bodyParser.json());

app.get('/answers', function(req, res, next){
fs.readFile(__dirname + '/../data/answers.json', function(err, data){
if(err) throw err;
next();
});
res.send(answers.toString());
});

app.post('/answers', function(req, res, next) {
req.on('data', function(data) {
var dataLogged = data.toString();
fs.writeFile(__dirname + '/../data/answers.json', dataLogged, function() {
if(err) throw err;
next();
});
});
res.send('POST request to the homepage');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

some folks generalized the requests a little more using parameters. They used /:item as the route and then used req.params.item in naming the file.


var server = app.listen(3000, function(){
console.log("The server is running on port 3000!");
});
43 changes: 43 additions & 0 deletions robin_stringer/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var mocha = require('gulp-mocha');
var appFiles = ['app.js', 'gulpfile.js'];
var testFiles = ['test/**/*.js'];

gulp.task('mocha:test', function() {
return gulp.src(testFiles, appFiles)
.pipe(mocha({
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true
}
}))
.pipe(mocha({reporter: 'nyan'}));
});

gulp.task('jshint:test', function() {
return gulp.src(testFiles)
.pipe(jshint({
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true
}
}))
.pipe(jshint.reporter('default'));
});

gulp.task('jshint:app', function() {
return gulp.src(appFiles)
.pipe(jshint({
node: true
}))
.pipe(jshint.reporter('default'));
});
gulp.task('jshint', ['jshint:test', 'jshint:app']);
gulp.task('default', ['jshint']);
29 changes: 29 additions & 0 deletions robin_stringer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "robin_stringer",
"version": "0.1.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rastringer/http_with_persistence.git"
},
"author": "ra1stringer@gmail.com",
"license": "MIT",
"bugs": {
"url": "https://github.com/rastringer/http_with_persistence/issues"
},
"homepage": "https://github.com/rastringer/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 needs to be a dependency along with express, not a devDependency

"chai": "^3.4.1",
"chai-http": "^1.0.0",
"gulp-jshint": "^1.12.0",
"gulp-mocha": "^2.1.3"
},
"dependencies": {
"express": "^4.13.3"
}
}
20 changes: 20 additions & 0 deletions robin_stringer/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var chai = require('chai');
var chaihttp = require('chai-http');
chai.use(chaihttp);
var expect = chai.expect;
var fs = require('fs');
require(__dirname + '/../app');

describe('the express server', function() {
it('should respond to POST requests and return values', function(done) {
chai.request('localhost:3000')
.post('/note_one')
.send('{noteBody: \'hello world\'}')
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like your app.js server is responding to this route (/noteBody). Is this test working?

.end(function(err, res) {
expect(err).to.eql(null);
expect(res.status).to.eql(200);
expect(res.text).to.eql('Thank you for your request');
done();
});
});
});