diff --git a/lib/app.js b/lib/app.js index 7635283..3b80cd3 100644 --- a/lib/app.js +++ b/lib/app.js @@ -10,6 +10,8 @@ const ensureAuth = require('./auth/ensure-auth')(); app.use(morgan('dev')); +// going forward, probably best just to `> npm i cors -S` and use that +// middleware app.use((req, res, next) => { console.log('setting cors headers'); const url = '*'; diff --git a/lib/auth/ensure-role.js b/lib/auth/ensure-role.js index 9b27e8b..7504854 100644 --- a/lib/auth/ensure-role.js +++ b/lib/auth/ensure-role.js @@ -1,3 +1,5 @@ +// don't leave unused files in project + module.exports = function getEnsureRole(...roles) { const lookup = roles.reduce((lookup, role) =>{ lookup[role] = true; diff --git a/lib/routes/pet-snapshots.js b/lib/routes/pet-snapshots.js index aaa47d5..1bb1a01 100644 --- a/lib/routes/pet-snapshots.js +++ b/lib/routes/pet-snapshots.js @@ -4,6 +4,14 @@ const petModel = require('../models/pet'); const petSnapshotModel = require('../models/pet-snapshot'); router + // best to put pet specific routes as part of parent url, ie: + + // /api/pets/:id/snapshots + + // and then still offer direct url for use with id: + + // /api/snapshots/:id + .get('/:animalId/all', bodyParser, (req, res, next) => { //get all data related to a specific pet const animalId = req.params.animalId; diff --git a/lib/routes/pets.js b/lib/routes/pets.js index 8f4fc0a..4a7d439 100644 --- a/lib/routes/pets.js +++ b/lib/routes/pets.js @@ -1,6 +1,7 @@ const router = require('express').Router(); const bodyParser = require('body-parser').json(); const userModel = require('../models/user'); +// I prefer: `Pet` because then `new Pet()` makes more sense const petModel = require('../models/pet'); router @@ -43,6 +44,7 @@ router .catch(next); }) .post('/', bodyParser, (req, res, next) => { + // nice destructuring :) const {_id, user} = req.user; req.body.usernameId = _id; req.body.owner = user; @@ -56,7 +58,7 @@ router petModel .findByIdAndUpdate(req.params.id, req.body, { new: true, - runValidators: true //not sure if need this + runValidators: true //not sure if need this. yes if you want updates to run schema validation! }) .then(savedPet => { res.send(savedPet); diff --git a/lib/routes/tests.js b/lib/routes/tests.js index 1eec7d6..12e6f09 100644 --- a/lib/routes/tests.js +++ b/lib/routes/tests.js @@ -1,3 +1,5 @@ +// not sure why this file is here... + const router = require('express').Router(); const bodyParser = require('body-parser').json(); diff --git a/lib/routes/users.js b/lib/routes/users.js index 63b99ac..de89d95 100644 --- a/lib/routes/users.js +++ b/lib/routes/users.js @@ -6,7 +6,7 @@ const ensureAuth = require('../auth/ensure-auth')(); const ensureRole = require('../auth/ensure-role'); router - .post('/validate', ensureAuth, bodyParser, (req, res, next) => { //eslint-disable-line + .post('/validate', ensureAuth, bodyParser, (req, res) => { res.send({valid:true, username: req.user.user}); }) .post('/signup', bodyParser, (req, res, next) => { diff --git a/test/test-setup-mongoose.js b/test/test-setup-mongoose.js index 296f05e..48b97e3 100644 --- a/test/test-setup-mongoose.js +++ b/test/test-setup-mongoose.js @@ -1,3 +1,8 @@ +// If you want to use this functionality in two places, +// change the export to a function that can be called with +// dbURI. Don't cut and paste entire file :( + + const mongoose = require( 'mongoose' ); // we need a URI that points to our database