From dec0a64a5acfca3895eb84f88b7e2f9c787a111a Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 13:18:17 -0700 Subject: [PATCH 1/7] scaffold --- .eslintignore | 5 ++ .eslintrc | 21 ++++++ .gitignore | 137 ++++++++++++++++++++++++++++++++++++++++ package.json | 24 +++++++ server.js | 1 + test/bake-route-test.js | 0 6 files changed, 188 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 package.json create mode 100644 server.js create mode 100644 test/bake-route-test.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..82ff623 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..b663d77 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95c57d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,137 @@ +# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows + +### macOS ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + +### OSX ### + +# Icon must end with two \r + +# Thumbnails + +# Files that might appear in the root of a volume + +# Directories potentially created on remote AFP share + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/osx,vim,node,macos,windows +Contact GitHub API Training Shop Blog About diff --git a/package.json b/package.json new file mode 100644 index 0000000..533ea56 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "11-express-api", + "version": "1.0.0", + "description": "![cf](https://i.imgur.com/7v5ASc8.png) 11: Single Resource Express API ======", + "main": "server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ohjonah/11-express-api.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ohjonah/11-express-api/issues" + }, + "homepage": "https://github.com/ohjonah/11-express-api#readme" +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..a726efc --- /dev/null +++ b/server.js @@ -0,0 +1 @@ +'use strict'; \ No newline at end of file diff --git a/test/bake-route-test.js b/test/bake-route-test.js new file mode 100644 index 0000000..e69de29 From 0fa5bcedd4729ba70d34fc2d971b2dba5ed31b74 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 13:19:16 -0700 Subject: [PATCH 2/7] scaffold --- lib/storage.js | 0 model/bake.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/storage.js create mode 100644 model/bake.js diff --git a/lib/storage.js b/lib/storage.js new file mode 100644 index 0000000..e69de29 diff --git a/model/bake.js b/model/bake.js new file mode 100644 index 0000000..e69de29 From 942e3cdb4abc82781c2669783abb40e75cef2eb8 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 13:21:27 -0700 Subject: [PATCH 3/7] adds dependencies --- package.json | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 533ea56..28e0c16 100644 --- a/package.json +++ b/package.json @@ -20,5 +20,16 @@ "bugs": { "url": "https://github.com/ohjonah/11-express-api/issues" }, - "homepage": "https://github.com/ohjonah/11-express-api#readme" + "homepage": "https://github.com/ohjonah/11-express-api#readme", + "devDependencies": { + "bluebird": "^3.5.0", + "body-parser": "^1.17.2", + "debug": "^2.6.8", + "http-errors": "^1.6.1", + "morgan": "^1.8.2", + "uuid": "^3.1.0" + }, + "dependencies": { + "express": "^4.15.3" + } } From 984fdbb12f98f5233cebaedece186c73c806f2c9 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 15:36:00 -0700 Subject: [PATCH 4/7] Modularizes and adds tests --- lib/storage.js | 49 +++++++++++++++++++++++ model/bake.js | 35 +++++++++++++++++ package.json | 7 +++- server.js | 58 ++++++++++++++++++++++++++- test/bake-route-test.js | 87 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 233 insertions(+), 3 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index e69de29..3aff135 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -0,0 +1,49 @@ +'use strict'; + +const Promise = require('bluebird'); +const fs = Promise.promisifyAll(require('fs'), { suffix: 'Prom' }); +const createError = require('http-errors'); +const debug = require('debug')('bake:storage'); + +module.exports = exports = {}; + +exports.createItem = function(schemaName, item) { + debug('create item'); + + if (!schemaName) return Promise.reject(createError(400, 'expected schema name')); + if (!item) return Promise.reject(createError(400, 'expected item')); + + let json = JSON.stringify(item); + return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${item.id}.json`, json) + .then( () => item) + .catch( err => Promise.reject(err)); +}; + +exports.fetchItem = function(schemaName, id) { + debug('fetch item'); + + if (!schemaName) return Promise.reject(createError(400, 'expected schema name')); + if (!id) return Promise.reject(createError(400, 'expected id')); + + return fs.readFileProm(`${__dirname}/../data/${schemaName}/${id}.json`) + .then( data => { + try { + let item = JSON.parse(data.toString()); + return item; + } catch (err) { + return Promise.reject(err); + } + }) + .catch( err => Promise.reject(err)); +}; + +exports.deleteItem = function(schemaName, id) { + debug('delete item'); + + if (!schemaName) return Promise.reject(createError(400, 'expected schema name')); + if (!id) return Promise.reject(createError(400, 'expected id')); + + return fs.unlinkProm(`${__dirname}/../data/${schemaName}/${id}.json`) + .then( () => console.log(`${id} deleted`)) + .catch( err => Promise.reject(err)); +}; diff --git a/model/bake.js b/model/bake.js index e69de29..9323b7a 100644 --- a/model/bake.js +++ b/model/bake.js @@ -0,0 +1,35 @@ +'use strict'; + +const uuidv4 = require('uuid/v4'); +const createError = require('http-errors'); +const debug = require('debug')('bake:bake'); +const storage = require('../lib/storage.js'); + +const Bake = module.exports = function(bakedGood, description, calories) { + if (!bakedGood) throw new createError(400, 'expected baked good'); + if (!description) throw new createError(400, 'expected description'); + if (!calories) throw new createError(400, 'expected expected calories'); + + this.id = uuidv4(); + this.bakedGood = bakedGood; + this.description = description; + this.calories = calories; +}; + +Bake.createBakedGood = function(_bake) { + debug('createBakedGood'); + + try { + let bake = new Bake(_bake.bakedGood, _bake.description, _bake.calories); + return storage.createItem('bake', bake); + } catch (err) { + return Promise.reject(err); + } +}; + +Bake.fetchBakedGood = function(id) { + debug('fetchBakedGood'); + + return storage.fetchItem('bake', id); +}; + diff --git a/package.json b/package.json index 28e0c16..9401abc 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "test": "test" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node server.js" + "test": "mocha", + "start": "DEBUG='bake*' node server.js" }, "repository": { "type": "git", @@ -24,9 +24,12 @@ "devDependencies": { "bluebird": "^3.5.0", "body-parser": "^1.17.2", + "chai": "^4.1.0", "debug": "^2.6.8", "http-errors": "^1.6.1", + "mocha": "^3.5.0", "morgan": "^1.8.2", + "superagent": "^3.5.2", "uuid": "^3.1.0" }, "dependencies": { diff --git a/server.js b/server.js index a726efc..cfdf6b6 100644 --- a/server.js +++ b/server.js @@ -1 +1,57 @@ -'use strict'; \ No newline at end of file +'use strict'; + + +const express = require('express'); +const morgan = require('morgan'); +const createError = require('http-errors'); +const jsonParser = require('body-parser').json(); +const debug = require('debug')('bake:server'); +const Note = require('./model/bake.js'); + +const PORT = process.env.PORT || 3000; +const app = express(); + +app.use(morgan('dev')); + +app.get('/test', function(req, res) { + debug('GET: /test'); + + res.json({ msg: 'hello from /test'}); +}); + +app.post('/api/bake', jsonParser, function(req, res, next) { + debug('POST: /api/bake'); + + Bake.createBakedGood(req.body) + .then( bake => res.json(bake)) + .catch( err => next(err)); + next(); +}); + +app.get('/api/bake', function(req, res, next) { + debug('GET: /api/bake'); + + Bake.fetchBakedGood(req.query.id) + .then( bake => res.json(bake)) + .catch( err => next(err)); +}); + +app.use(function(err, req, res) { + debug('error middleware'); + console.error(err.message); + + if (err.status) { + res.status(err.status).send(err.name); + return; + } + + err = createError(500, err.message); + res.status(err.status).send(err.name); +}); + + + + +app.listen(PORT, () => { + debug(`Server listening on PORT: ${PORT}`); +}); \ No newline at end of file diff --git a/test/bake-route-test.js b/test/bake-route-test.js index e69de29..4cfd248 100644 --- a/test/bake-route-test.js +++ b/test/bake-route-test.js @@ -0,0 +1,87 @@ +'use strict'; + +const request = require('superagent'); +const expect = require('chai').expect; + +require('../server.js'); + +describe('Baked Good Routes', function() { + var bake = null; + + describe('POST: 400/Bad Request', function() { + it('should return 400', done => { + request.post('localhost:8000/api/bake') + .end((err, res) => { + expect(res.status).to.equal(400); + done(); + }); + }); + }); + + describe('POST: /api/bake', function() { + it('should make a baked good', function(done) { + request.post('localhost:8000/api/bake') + .send({ + bakedGood: 'muffin', + description: 'naked cupcake', + calories: 255 + }) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.bakedGood).to.equal('muffin'); + expect(res.body.description).to.equal('naked cupcake'); + expect(res.body.calories).to.equal(255); + + bake = res.body; + done(); + }); + }); + }); + + describe('GET: /api/bake', function() { + it('should return a baked good', function(done) { + request.get(`localhost:8000/api/bake?id=${bake.id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.bakedGood).to.equal('muffin'); + expect(res.body.description).to.equal('naked cupcake'); + expect(res.body.calories).to.equal(255); + done(); + }); + }); + }); + + describe('GET: 404/Unregistered Route', () => { + it('should return a 404', done => { + request.get('localhost:8000/api/baykk') + .end((err, res) => { + expect(res.status).to.equal(404); + done(); + }); + }); + }); + + describe('GET: 400/No ID', () => { + it('should return a 400', done => { + request.get('localhost:8000/api/bake') + .end((err, res) => { + expect(res.status).to.equal(400); + done(); + }); + }); + }); + + describe('DELETE: /api/bake', function() { + it('should delete a baked good', function(done) { + request.delete(`localhost:8000/api/bake?id=${bake.id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(204); + expect(res.body.bakedGood).to.equal(undefined); + done(); + }); + }); + }); +}); \ No newline at end of file From 867f4d1b141da1ebb64f5a767a67d6436af052e6 Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 16:09:50 -0700 Subject: [PATCH 5/7] Troubleshooting --- .../09af079d-9a2b-4a72-a138-b09cb9298a26.json | 1 + .../fd589bad-e075-405e-ba67-c935525af964.json | 1 + server.js | 9 +- test/bake-route-test.js | 154 ++++++++++-------- 4 files changed, 87 insertions(+), 78 deletions(-) create mode 100644 data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json create mode 100644 data/bake/fd589bad-e075-405e-ba67-c935525af964.json diff --git a/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json b/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json new file mode 100644 index 0000000..4f48f1c --- /dev/null +++ b/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json @@ -0,0 +1 @@ +{"id":"09af079d-9a2b-4a72-a138-b09cb9298a26","bakedGood":"muffin","description":"naked cupcake","calories":255} \ No newline at end of file diff --git a/data/bake/fd589bad-e075-405e-ba67-c935525af964.json b/data/bake/fd589bad-e075-405e-ba67-c935525af964.json new file mode 100644 index 0000000..db95691 --- /dev/null +++ b/data/bake/fd589bad-e075-405e-ba67-c935525af964.json @@ -0,0 +1 @@ +{"id":"fd589bad-e075-405e-ba67-c935525af964","bakedGood":"Fig Bar","description":"celebratory","calories":"90"} \ No newline at end of file diff --git a/server.js b/server.js index cfdf6b6..5d264b7 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ const morgan = require('morgan'); const createError = require('http-errors'); const jsonParser = require('body-parser').json(); const debug = require('debug')('bake:server'); -const Note = require('./model/bake.js'); +const Bake = require('./model/bake.js'); const PORT = process.env.PORT || 3000; const app = express(); @@ -36,7 +36,7 @@ app.get('/api/bake', function(req, res, next) { .catch( err => next(err)); }); -app.use(function(err, req, res) { +app.use(function(err, req, res, next) { debug('error middleware'); console.error(err.message); @@ -49,9 +49,6 @@ app.use(function(err, req, res) { res.status(err.status).send(err.name); }); - - - app.listen(PORT, () => { - debug(`Server listening on PORT: ${PORT}`); + console.log(`Server listening on PORT: ${PORT}`); }); \ No newline at end of file diff --git a/test/bake-route-test.js b/test/bake-route-test.js index 4cfd248..47c822b 100644 --- a/test/bake-route-test.js +++ b/test/bake-route-test.js @@ -6,82 +6,92 @@ const expect = require('chai').expect; require('../server.js'); describe('Baked Good Routes', function() { - var bake = null; + // var bake = null; - describe('POST: 400/Bad Request', function() { - it('should return 400', done => { - request.post('localhost:8000/api/bake') + describe('GET: /test', function() { + it('should return msg', done => { + request.get('localhost:8000/test') .end((err, res) => { - expect(res.status).to.equal(400); - done(); - }); - }); - }); - - describe('POST: /api/bake', function() { - it('should make a baked good', function(done) { - request.post('localhost:8000/api/bake') - .send({ - bakedGood: 'muffin', - description: 'naked cupcake', - calories: 255 - }) - .end((err, res) => { - if (err) return done(err); - expect(res.status).to.equal(200); - expect(res.body.bakedGood).to.equal('muffin'); - expect(res.body.description).to.equal('naked cupcake'); - expect(res.body.calories).to.equal(255); - - bake = res.body; - done(); - }); - }); - }); - - describe('GET: /api/bake', function() { - it('should return a baked good', function(done) { - request.get(`localhost:8000/api/bake?id=${bake.id}`) - .end((err, res) => { - if (err) return done(err); - expect(res.status).to.equal(200); - expect(res.body.bakedGood).to.equal('muffin'); - expect(res.body.description).to.equal('naked cupcake'); - expect(res.body.calories).to.equal(255); - done(); - }); - }); - }); - - describe('GET: 404/Unregistered Route', () => { - it('should return a 404', done => { - request.get('localhost:8000/api/baykk') - .end((err, res) => { - expect(res.status).to.equal(404); - done(); - }); - }); - }); - - describe('GET: 400/No ID', () => { - it('should return a 400', done => { - request.get('localhost:8000/api/bake') - .end((err, res) => { - expect(res.status).to.equal(400); - done(); - }); - }); - }); - - describe('DELETE: /api/bake', function() { - it('should delete a baked good', function(done) { - request.delete(`localhost:8000/api/bake?id=${bake.id}`) - .end((err, res) => { - if (err) return done(err); - expect(res.status).to.equal(204); - expect(res.body.bakedGood).to.equal(undefined); + expect(res.status.to.equal(200)); done(); }); }); }); + // + // describe('POST: 400/Bad Request', function() { + // it('should return 400', done => { + // request.post('localhost:8000/api/bake') + // .end((err, res) => { + // expect(res.status).to.equal(400); + // done(); + // }); + // }); + // }); + // + // describe('POST: /api/bake', function() { + // it('should make a baked good', function(done) { + // request.post('localhost:8000/api/bake') + // .send({ + // bakedGood: 'muffin', + // description: 'naked cupcake', + // calories: 255 + // }) + // .end((err, res) => { + // if (err) return done(err); + // expect(res.status).to.equal(200); + // expect(res.body.bakedGood).to.equal('muffin'); + // expect(res.body.description).to.equal('naked cupcake'); + // expect(res.body.calories).to.equal(255); + // + // bake = res.body; + // done(); + // }); + // }); + // }); + // + // describe('GET: /api/bake', function() { + // it('should return a baked good', function(done) { + // request.get(`localhost:8000/api/bake?id=${bake.id}`) + // .end((err, res) => { + // if (err) return done(err); + // expect(res.status).to.equal(200); + // expect(res.body.bakedGood).to.equal('muffin'); + // expect(res.body.description).to.equal('naked cupcake'); + // expect(res.body.calories).to.equal(255); + // done(); + // }); + // }); + // }); + // + // describe('GET: 404/Unregistered Route', () => { + // it('should return a 404', done => { + // request.get('localhost:8000/api/baykk') + // .end((err, res) => { + // expect(res.status).to.equal(404); + // done(); + // }); + // }); + // }); + // + // describe('GET: 400/No ID', () => { + // it('should return a 400', done => { + // request.get('localhost:8000/api/bake') + // .end((err, res) => { + // expect(res.status).to.equal(400); + // done(); + // }); + // }); + // }); + // + // describe('DELETE: /api/bake', function() { + // it('should delete a baked good', function(done) { + // request.delete(`localhost:8000/api/bake?id=${bake.id}`) + // .end((err, res) => { + // if (err) return done(err); + // expect(res.status).to.equal(204); + // expect(res.body.bakedGood).to.equal(undefined); + // done(); + // }); + // }); + // }); }); \ No newline at end of file From 102963e48346424bd6576cb9e510fa17ea39c85e Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 23:09:20 -0700 Subject: [PATCH 6/7] Adds Delete Method and Testing --- .../09af079d-9a2b-4a72-a138-b09cb9298a26.json | 1 - .../b78e48ca-3201-4aeb-8f05-158a6bfab915.json | 1 + .../fd589bad-e075-405e-ba67-c935525af964.json | 1 - lib/storage.js | 6 +- model/bake.js | 12 +- server.js | 13 +- test/bake-route-test.js | 160 +++++++++--------- 7 files changed, 105 insertions(+), 89 deletions(-) delete mode 100644 data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json create mode 100644 data/bake/b78e48ca-3201-4aeb-8f05-158a6bfab915.json delete mode 100644 data/bake/fd589bad-e075-405e-ba67-c935525af964.json diff --git a/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json b/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json deleted file mode 100644 index 4f48f1c..0000000 --- a/data/bake/09af079d-9a2b-4a72-a138-b09cb9298a26.json +++ /dev/null @@ -1 +0,0 @@ -{"id":"09af079d-9a2b-4a72-a138-b09cb9298a26","bakedGood":"muffin","description":"naked cupcake","calories":255} \ No newline at end of file diff --git a/data/bake/b78e48ca-3201-4aeb-8f05-158a6bfab915.json b/data/bake/b78e48ca-3201-4aeb-8f05-158a6bfab915.json new file mode 100644 index 0000000..0c9136c --- /dev/null +++ b/data/bake/b78e48ca-3201-4aeb-8f05-158a6bfab915.json @@ -0,0 +1 @@ +{"id":"b78e48ca-3201-4aeb-8f05-158a6bfab915","bakedGood":"muffin","description":"naked cupcake","calories":255} \ No newline at end of file diff --git a/data/bake/fd589bad-e075-405e-ba67-c935525af964.json b/data/bake/fd589bad-e075-405e-ba67-c935525af964.json deleted file mode 100644 index db95691..0000000 --- a/data/bake/fd589bad-e075-405e-ba67-c935525af964.json +++ /dev/null @@ -1 +0,0 @@ -{"id":"fd589bad-e075-405e-ba67-c935525af964","bakedGood":"Fig Bar","description":"celebratory","calories":"90"} \ No newline at end of file diff --git a/lib/storage.js b/lib/storage.js index 3aff135..ab17ccd 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -16,7 +16,7 @@ exports.createItem = function(schemaName, item) { let json = JSON.stringify(item); return fs.writeFileProm(`${__dirname}/../data/${schemaName}/${item.id}.json`, json) .then( () => item) - .catch( err => Promise.reject(err)); + .catch( () => Promise.reject(createError(400, 'unable to write file'))); }; exports.fetchItem = function(schemaName, id) { @@ -31,10 +31,10 @@ exports.fetchItem = function(schemaName, id) { let item = JSON.parse(data.toString()); return item; } catch (err) { - return Promise.reject(err); + return Promise.reject(createError(404, 'expected SOMETHING')); } }) - .catch( err => Promise.reject(err)); + .catch( () => Promise.reject(createError(404, 'no file found'))); }; exports.deleteItem = function(schemaName, id) { diff --git a/model/bake.js b/model/bake.js index 9323b7a..293f93e 100644 --- a/model/bake.js +++ b/model/bake.js @@ -6,9 +6,9 @@ const debug = require('debug')('bake:bake'); const storage = require('../lib/storage.js'); const Bake = module.exports = function(bakedGood, description, calories) { - if (!bakedGood) throw new createError(400, 'expected baked good'); - if (!description) throw new createError(400, 'expected description'); - if (!calories) throw new createError(400, 'expected expected calories'); + if (!bakedGood) throw createError(400, 'expected baked good'); + if (!description) throw createError(400, 'expected description'); + if (!calories) throw createError(400, 'expected expected calories'); this.id = uuidv4(); this.bakedGood = bakedGood; @@ -33,3 +33,9 @@ Bake.fetchBakedGood = function(id) { return storage.fetchItem('bake', id); }; +Bake.deleteBakedGood = function(id) { + debug('deleteBakedGood'); + + return storage.deleteItem('bake', id); +}; + diff --git a/server.js b/server.js index 5d264b7..8930fee 100644 --- a/server.js +++ b/server.js @@ -25,7 +25,6 @@ app.post('/api/bake', jsonParser, function(req, res, next) { Bake.createBakedGood(req.body) .then( bake => res.json(bake)) .catch( err => next(err)); - next(); }); app.get('/api/bake', function(req, res, next) { @@ -36,6 +35,16 @@ app.get('/api/bake', function(req, res, next) { .catch( err => next(err)); }); +app.delete('/api/bake', function(req, res, next) { + debug('DELETE: /api/bake'); + + Bake.deleteBakedGood(req.query.id) + .then( () => { + res.status(204).end(); + }) + .catch( err => next(err)); +}); + app.use(function(err, req, res, next) { debug('error middleware'); console.error(err.message); @@ -50,5 +59,5 @@ app.use(function(err, req, res, next) { }); app.listen(PORT, () => { - console.log(`Server listening on PORT: ${PORT}`); + debug(`Server listening on PORT: ${PORT}`); }); \ No newline at end of file diff --git a/test/bake-route-test.js b/test/bake-route-test.js index 47c822b..d8e2ae3 100644 --- a/test/bake-route-test.js +++ b/test/bake-route-test.js @@ -6,92 +6,94 @@ const expect = require('chai').expect; require('../server.js'); describe('Baked Good Routes', function() { - // var bake = null; + var bake = null; describe('GET: /test', function() { it('should return msg', done => { request.get('localhost:8000/test') .end((err, res) => { - expect(res.status.to.equal(200)); + expect(res.status).to.equal(200); + done(); + }); + }); + }); + + describe('POST: 400/Bad Request', function() { + it('should return 400', done => { + request.post('localhost:8000/api/bake') + .end((err, res) => { + expect(res.status).to.equal(400); + done(); + }); + }); + }); + + describe('POST: /api/bake', function() { + it('should make a baked good', function(done) { + request.post('localhost:8000/api/bake') + .send({ + bakedGood: 'muffin', + description: 'naked cupcake', + calories: 255 + }) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.bakedGood).to.equal('muffin'); + expect(res.body.description).to.equal('naked cupcake'); + expect(res.body.calories).to.equal(255); + + bake = res.body; + done(); + }); + }); + }); + + describe('GET: /api/bake', function() { + it('should return a baked good', function(done) { + request.get(`localhost:8000/api/bake?id=${bake.id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(200); + expect(res.body.bakedGood).to.equal('muffin'); + expect(res.body.description).to.equal('naked cupcake'); + expect(res.body.calories).to.equal(255); + done(); + }); + }); + }); + + describe('GET: 404/Unregistered Route', () => { + it('should return a 404', done => { + request.get('localhost:8000/api/baykk') + .end((err, res) => { + expect(res.status).to.equal(404); + done(); + }); + }); + }); + + describe('GET: 400/No ID', () => { + it('should return a 400', done => { + request.get('localhost:8000/api/bake') + .end((err, res) => { + expect(res.status).to.equal(400); + done(); + }); + }); + }); + + describe('DELETE: /api/bake', function() { + it('should delete a baked good', done => { + request.delete(`localhost:8000/api/bake?id=${bake.id}`) + .end((err, res) => { + if (err) return done(err); + expect(res.status).to.equal(204); + console.log('HDY HDSHDZLKVJSDLZKJVKZSJDV'); + expect(res.body.bakedGood).to.equal(undefined); + console.log('ZZZZZZZZZZZ'); done(); }); }); }); - // - // describe('POST: 400/Bad Request', function() { - // it('should return 400', done => { - // request.post('localhost:8000/api/bake') - // .end((err, res) => { - // expect(res.status).to.equal(400); - // done(); - // }); - // }); - // }); - // - // describe('POST: /api/bake', function() { - // it('should make a baked good', function(done) { - // request.post('localhost:8000/api/bake') - // .send({ - // bakedGood: 'muffin', - // description: 'naked cupcake', - // calories: 255 - // }) - // .end((err, res) => { - // if (err) return done(err); - // expect(res.status).to.equal(200); - // expect(res.body.bakedGood).to.equal('muffin'); - // expect(res.body.description).to.equal('naked cupcake'); - // expect(res.body.calories).to.equal(255); - // - // bake = res.body; - // done(); - // }); - // }); - // }); - // - // describe('GET: /api/bake', function() { - // it('should return a baked good', function(done) { - // request.get(`localhost:8000/api/bake?id=${bake.id}`) - // .end((err, res) => { - // if (err) return done(err); - // expect(res.status).to.equal(200); - // expect(res.body.bakedGood).to.equal('muffin'); - // expect(res.body.description).to.equal('naked cupcake'); - // expect(res.body.calories).to.equal(255); - // done(); - // }); - // }); - // }); - // - // describe('GET: 404/Unregistered Route', () => { - // it('should return a 404', done => { - // request.get('localhost:8000/api/baykk') - // .end((err, res) => { - // expect(res.status).to.equal(404); - // done(); - // }); - // }); - // }); - // - // describe('GET: 400/No ID', () => { - // it('should return a 400', done => { - // request.get('localhost:8000/api/bake') - // .end((err, res) => { - // expect(res.status).to.equal(400); - // done(); - // }); - // }); - // }); - // - // describe('DELETE: /api/bake', function() { - // it('should delete a baked good', function(done) { - // request.delete(`localhost:8000/api/bake?id=${bake.id}`) - // .end((err, res) => { - // if (err) return done(err); - // expect(res.status).to.equal(204); - // expect(res.body.bakedGood).to.equal(undefined); - // done(); - // }); - // }); - // }); }); \ No newline at end of file From 5f7ceb098efd5893f6641605cf44554973474add Mon Sep 17 00:00:00 2001 From: ohjonah Date: Mon, 31 Jul 2017 23:46:21 -0700 Subject: [PATCH 7/7] Adds rudimentary readme --- README.md | 56 +++++------------------------------------ test/bake-route-test.js | 2 -- 2 files changed, 6 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index feb511f..9371c4f 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,9 @@ -![cf](https://i.imgur.com/7v5ASc8.png) 11: Single Resource Express API -====== +# Baked Goods API -## Submission Instructions - * fork this repository & create a new branch for your work - * write all of your code in a directory named `lab-` + `` **e.g.** `lab-susan` - * push to your repository - * submit a pull request to this repository - * submit a link to your PR in canvas - * write a question and observation on canvas +This REST API uses express to route incoming requests. -## Learning Objectives -* students will be able to create a single resource API using the express framework -* students will be able to leverage 3rd party helper modules for debugging, logging, and handling errors +# Endpoints -## Requirements - -#### Configuration -* `package.json` -* `.eslintrc` -* `.gitignore` -* `README.md` - * your `README.md` should include detailed instructions on how to use your API - -#### Feature Tasks -* create an HTTP server using `express` -* create a object constructor that creates a _simple resource_ with at least 3 properties - * it can **not** have the same properties as the in-class sample code (other than the `id`) - * a unique `id` property should be included *(node-uuid)* - * include two additional properties of your choice -* use the JSON parser included with the `body-parser` module as a middleware component to parse the request body on `POST` and `PUT` routes -* use the npm `debug` module to log the methods in your application -* create an `npm` script to automate the `debug` process and start the server -* persist your API data using the storage module and file system persistence - -#### Server Endpoints -* **`/api/simple-resource-name`** -* `POST` request - * pass data as stringifed JSON in the body of a **POST** request to create a new resource -* `GET` request - * pass `?id=` as a query string parameter to retrieve a specific resource (as JSON) -* `DELETE` request - * pass `?id=` in the query string to **DELETE** a specific resource - * this should return a 204 status code with no content in the body - -#### Tests -* write a test to ensure that your api returns a status code of 404 for routes that have not been registered -* write tests to ensure the `/api/simple-resource-name` endpoint responds as described for each condition below: - * `GET`: test 404, it should respond with 'not found' for valid requests made with an id that was not found - * `GET`: test 400, it should respond with 'bad request' if no id was provided in the request - * `GET`: test 200, it should contain a response body for a request made with a valid id - * `POST`: test 400, it should respond with 'bad request' if no request body was provided or the body was invalid - * `POST`: test 200, it should respond with the body content for a post request with a valid body +**GET** : '/' +**POST** : '/api/bake?id= +**DELETE** : '/api/bake?id= diff --git a/test/bake-route-test.js b/test/bake-route-test.js index d8e2ae3..4020775 100644 --- a/test/bake-route-test.js +++ b/test/bake-route-test.js @@ -89,9 +89,7 @@ describe('Baked Good Routes', function() { .end((err, res) => { if (err) return done(err); expect(res.status).to.equal(204); - console.log('HDY HDSHDZLKVJSDLZKJVKZSJDV'); expect(res.body.bakedGood).to.equal(undefined); - console.log('ZZZZZZZZZZZ'); done(); }); });