-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (55 loc) · 1.61 KB
/
index.js
File metadata and controls
73 lines (55 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// TODO: user app.params to find the lion using the id
// and then attach the lion to the req object and call next. Then in
// '/lion/:id' just send back req.lion
// create a middleware function to catch and handle errors, register it
// as the last middleware on app
// create a route middleware for POST /lions that will increment and
// add an id to the incoming new lion object on req.body
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var _ = require('lodash');
var morgan = require('morgan');
var lions = [];
var id = 0;
var updateId = function(req, res, next) {
// fill this out. this is the route middleware for the ids
};
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()
id++;
console.log({id})
next();
});
app.get('/lions', function(req, res){
res.json(lions);
});
app.get('/lions/:id', function(req, res){
// use req.lion
res.json(lion || {});
});
app.post('/lions', updateId, function(req, res) {
var lion = req.body;
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);
}
});
app.listen(3000);
console.log('on port 3000');