-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.js
37 lines (27 loc) · 1 KB
/
middlewares.js
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
const ordersList = require('./index'); // ordersList.ordersList === index ordersList
const showMethodNUrl = (req, res, next) => {
console.log(`${req.method}: ${req.url}`);
next();
};
const checkIdExistence = (req, res, next) => {
// finding order by id
const orderId = req.params.id;
const orderIndex = ordersList.ordersList.findIndex(order => order.id === orderId);
// return error if not found
if (orderIndex < 0) {
return res.status(404).json({ error: 'Order Not Found' });
}
// incorporating the order index in request
req.orderIndex = orderIndex;
next();
};
// verify if client is trying to change or add id or status information
const verifyClientData = (req, res, next) => {
const { status, id } = req.body;
if (status || id) {
return res.status(401).json({ error: 'Unable to change id or status information' });
}
next();
};
// exporting middlewares to index.js
module.exports = { showMethodNUrl, checkIdExistence, verifyClientData };