-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniExpress_middlewares.js
103 lines (85 loc) · 2.76 KB
/
miniExpress_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
93
94
95
96
97
98
99
100
101
102
103
var querystring = require("querystring");
var path = require("path");
function static(rootFolder) {
if (!rootFolder) {
throw new Error('static() root path required');
}
rootFolder = path.normalize(rootFolder);
return function (req, res, next) {
if ('get' !== req.method) {
next();
return;
}
var requestedResourceURL = req.path.replace(/%20/g,' ');
res.resourcePath = path.normalize(requestedResourceURL.replace(req.route.regexp.exec(requestedResourceURL)[1], rootFolder));
// Check if the resource is under the root folder
if (path.relative(rootFolder, res.resourcePath).indexOf("..") === 0) {
res.send(404, "Illegal resource (Not under root folder)");
return;
}
res.writeFile();
};
};
function cookieParser() {
cookieParser.isBeingUsed = true;
return function (req, res, next) {
var cookieHeader = req.get("cookie");
if (cookieHeader) {
req.cookies = querystring.parse(cookieHeader, /\s*;\s*/);
}
if (next) {
next();
}
};
}
cookieParser.isBeingUsed = false;
function json() {
return function (req, res, next) {
if (req.is("json")) {
req.body = JSON.parse(req.body);
}
if (next) {
next();
}
}
}
function urlencoded() {
return function (req, res, next) {
if (req.is('application/x-www-form-urlencoded')) {
var body = req.body;
var query = querystring.parse(body, "&");
// Keys can be in the format of objName[propName]. This loop searches for such keys, if found the keys get replaced by the proper object.
for (key in query) {
var keyInfo = /^(.+)\[(.+)\]$/.exec(key);
if (keyInfo != null) {
var objName = keyInfo[1];
var properyName = keyInfo[2];
if (!(objName in query)) {
query[objName] = {};
}
query[objName][properyName] = query[key];
delete query[key];
}
}
req.body = query;
}
if (next) {
next();
}
}
}
function bodyParser() {
return function (req, res, next) {
// we don't pass next cause we don't want them to call next
json()(req, res);
urlencoded()(req, res);
if (next) {
next();
}
}
}
exports.static = static;
exports.cookieParser = cookieParser;
exports.json = json;
exports.urlencoded = urlencoded;
exports.bodyParser = bodyParser;