-
Notifications
You must be signed in to change notification settings - Fork 19
Assignment for Robin Stringer #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
94e6b8d
a8b990d
9546621
a39c3fc
a57228d
4816b0b
0ba43b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| **/*.sw? | ||
| **/node_modules | ||
| **/data | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules | ||
| data | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good! |
||
| 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 |
| 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'); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
| }); | ||
| 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']); |
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
| } | ||
| } | ||
| 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\'}') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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.