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/robin_stringer/.DS_Store b/robin_stringer/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/robin_stringer/.DS_Store differ diff --git a/robin_stringer/.gitignore b/robin_stringer/.gitignore new file mode 100644 index 0000000..aa15018 --- /dev/null +++ b/robin_stringer/.gitignore @@ -0,0 +1,2 @@ +node_modules +data \ No newline at end of file diff --git a/robin_stringer/README.md b/robin_stringer/README.md new file mode 100644 index 0000000..a35695b --- /dev/null +++ b/robin_stringer/README.md @@ -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 diff --git a/robin_stringer/app.js b/robin_stringer/app.js new file mode 100644 index 0000000..d185eb0 --- /dev/null +++ b/robin_stringer/app.js @@ -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'); +}); + +var server = app.listen(3000, function(){ + console.log("The server is running on port 3000!"); +}); diff --git a/robin_stringer/gulpfile.js b/robin_stringer/gulpfile.js new file mode 100644 index 0000000..6c15ffa --- /dev/null +++ b/robin_stringer/gulpfile.js @@ -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']); diff --git a/robin_stringer/package.json b/robin_stringer/package.json new file mode 100644 index 0000000..026e7f5 --- /dev/null +++ b/robin_stringer/package.json @@ -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" + "chai": "^3.4.1", + "chai-http": "^1.0.0", + "gulp-jshint": "^1.12.0", + "gulp-mocha": "^2.1.3" + }, + "dependencies": { + "express": "^4.13.3" + } +} diff --git a/robin_stringer/test/test.js b/robin_stringer/test/test.js new file mode 100644 index 0000000..88ea343 --- /dev/null +++ b/robin_stringer/test/test.js @@ -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\'}') + .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(); + }); +}); +});