-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
61 lines (57 loc) · 1.78 KB
/
middleware.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { getQuerySchema, postFormSchema } = require("./validationSchemas");
const ExpressError = require("./utils/ExpressError");
module.exports.validateGet = (req, res, next) => {
// const { error } = ipSchema.validate(req.query.ip);
const { error } = getQuerySchema.validate(req.query);
if (error) {
const msg = error.details.map((el) => el.message).join(",");
console.log(msg);
throw new ExpressError(msg, 400);
} else {
next();
}
};
module.exports.validatePost = (req, res, next) => {
const { error } = postFormSchema.validate(req.body);
if (error) {
const msg = error.details.map((el) => el.message).join(",");
throw new ExpressError(msg, 400);
} else {
next();
}
};
module.exports.getLogParams = (query) => {
const limit = 20;
const page = parseInt(query.page) || 1;
let date;
let condition = {}; //search conditions for pagination
//path needs to be stored in order to track search query string when paginating
let path = "/logs?";
if (query.date) {
date = query.date;
path = path + `date=${query.date}`;
const startSearchDate = new Date(date);
const endSearchDate = new Date(startSearchDate.getTime() + 1000 * 60 * 60 * 24);
condition.time = { $gte: startSearchDate, $lt: endSearchDate };
}
if (query.ip) {
path = path + `&ip=${query.ip}`;
condition.userIP = query.ip;
}
const paginateOptions = { page: page, limit: limit, sort: { _id: -1 } };
return {path, condition, paginateOptions};
};
/**
* Dummy response delay inserter middleware, for example to test spinners in frontend
* @param req
* @param res
* @param next
*/
module.exports.delayer = (req, res, next) => {
const delay = 3000;
setTimeout(() => {
console.log(`this was a dummy delay of ${delay} ms.`);
next()
},
delay);
};