-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.js
More file actions
30 lines (28 loc) · 892 Bytes
/
methods.js
File metadata and controls
30 lines (28 loc) · 892 Bytes
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
const _m = {
"get": "GET",
"post": "POST",
"patch": "PATCH",
"delete": "DELETE",
"update": "UPDATE"
}
const setMethodMiddlewares = (obj) => {
for(let item in _m) {
obj[item] = (path, middleware) => {
++obj.middleWareCount
const key = String(obj.middleWareCount)
const middlewares = obj.middlewares
middlewares[key] = {}
middlewares[key]["function"] = async (req, res, next) => {
if (req.method === _m[item] && req.path === path) {
await middleware(req, res, next)
} else {
next()
}
}
middlewares[key]["order"] = String(obj.middleWareCount + 1)
middlewares[key]["description"] = item + path
}
}
return obj
}
exports.setMethodMiddlewares = setMethodMiddlewares