From 1e91ebf49f90cb32d6b7e22899039b1a6ae3c3db Mon Sep 17 00:00:00 2001 From: Loreen-netizen Date: Thu, 4 Feb 2021 16:57:01 +0200 Subject: [PATCH 1/2] using req.body and req.params and creating route middleware --- index.js | 78 ++++++++++++++++++++++++++++++++++ server/{server.js => index.js} | 3 ++ 2 files changed, 81 insertions(+) create mode 100644 index.js rename server/{server.js => index.js} (97%) diff --git a/index.js b/index.js new file mode 100644 index 0000000..1a70b63 --- /dev/null +++ b/index.js @@ -0,0 +1,78 @@ +// create a middleware function to catch and handle errors, register it +// as the last middleware on app + + + +var express = require('express'); +var bodyParser = require('body-parser'); +var app = express(); +var _ = require('lodash'); +var morgan = require('morgan'); + +var lions = [{name: "mimi", pride: "wakanda", age: 3, gender: "male"}]; +var id = 0; + +var updateId = function(req, res, next) { + // fill this out. this is the route middleware for the ids + id = id++ + next(id); +}; + +app.use(morgan('dev')) +app.use(express.static('client')); +app.use(bodyParser.urlencoded({extended: true})); +app.use(bodyParser.json()); + + +app.param('id', function(req, res, next, id) { + // fill this out to find the lion based off the id + // and attach it to req.lion. Rember to call next() + lion = id; + + console.log({lion}) + next(); +}); + +app.get('/lions', function(req, res){ + res.json(lions); +}); + +app.get('/lions/:id', function(req, res){ + req.lion + let theLion = lions[lion]; + console.log({theLion}); + res.json(theLion || {}); +}); + +app.post('/lions', updateId, function(req, res) { + var lion = req.body; +console.log({lion}) + lions.push(lion); + + res.json(lion); +}); + + +app.put('/lions/:id', function(req, res) { + var update = req.body; + if (update.id) { + delete update.id + } + + var lion = _.findIndex(lions, {id: req.params.id}); + if (!lions[lion]) { + res.send(); + } else { + var updatedLion = _.assign(lions[lion], update); + res.json(updatedLion); + } +}); + + +function errors (err,req,res,next){ + req.err(console.log("error message")) + next(); + }; + +app.listen(3000); +console.log('on port 3000'); diff --git a/server/server.js b/server/index.js similarity index 97% rename from server/server.js rename to server/index.js index 40026e7..e21193d 100644 --- a/server/server.js +++ b/server/index.js @@ -31,6 +31,9 @@ app.use(bodyParser.json()); app.param('id', function(req, res, next, id) { // fill this out to find the lion based off the id // and attach it to req.lion. Rember to call next() + id++; + console.log({id}) + next(); }); app.get('/lions', function(req, res){ From 2ecdb50129e822a94e91aceb774ff1a2ecd68636 Mon Sep 17 00:00:00 2001 From: Loreen-netizen Date: Mon, 8 Feb 2021 14:45:02 +0200 Subject: [PATCH 2/2] index.js update --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1a70b63..caab75d 100644 --- a/index.js +++ b/index.js @@ -69,10 +69,10 @@ app.put('/lions/:id', function(req, res) { }); -function errors (err,req,res,next){ - req.err(console.log("error message")) - next(); - }; +app.use(function(err,req,res,next){ + if(err){(console.log("error message"))} + res.status(500).send(err) + }); app.listen(3000); console.log('on port 3000');