-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.js
92 lines (80 loc) · 2.39 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
exports.allowCrossDomain = function allowCrossDomain(req, res, next) {
// let allowHeaders = DEFAULT_ALLOWED_HEADERS;
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
// res.header('Access-Control-Allow-Headers', allowHeaders);
res.header(
"Access-Control-Expose-Headers",
"X-Parse-Job-Status-Id, X-Parse-Push-Status-Id"
); // intercept OPTIONS method
if ("OPTIONS" == req.method) {
res.sendStatus(200);
} else {
next();
}
};
exports.allowMethodOverride = function allowMethodOverride(req, res, next) {
if (req.method === "POST" && req.body._method) {
req.originalMethod = req.method;
req.method = req.body._method;
delete req.body._method;
}
next();
};
exports.error404Handler = function error404Handler(error, req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err) || next(createError(404));
};
exports.errorHandler = function errorHandler(err, req, res, next) {
// render the error page
res.status(err.status || 500);
var html = "<!DOCTYPE html>";
html += "<html>";
html += " <head>";
html += " <title></title>";
html += " </head>";
html += " <body>";
html += " <h1>" + err.message + "</h1>";
html += " <h2>" + err.status + "</h2>";
html += " <h2>More information: [email protected]</h2>";
html += " <pre>" + err.stack + "</pre>";
html += " </body>";
html += "</html>";
res.send(html);
};
exports.errorLogHandler = function errorLogHandler(err, req, res, next) {
logger.error(
`${req.method} - ${err.message} - ${req.originalUrl} - ${req.ip}`
);
next(err);
};
exports.fixDateFormatOnProdDB = function fixDateFormatOnProdDB() {
Stock.find({
// _id: mongoose.mongo.ObjectId("633a925da76dd590ada1d70c"),
// date: new Date("10/03/2022"),
code: "STO",
})
.then((res) => {
return Promise.all(
res.map((data) => {
// if (date)
// data.date = new Date(data.date)
// data.save()
return data;
})
);
})
.then((res) => {
console.table(res);
});
};
// fixDateFormatOnProdDB()
exports.parallel = async function parallel(arr, fn, threads = 2) {
const result = [];
while (arr.length) {
const res = await Promise.all(arr.splice(0, threads).map((x) => fn(x)));
result.push(res);
}
return result.flat();
};