-
Notifications
You must be signed in to change notification settings - Fork 0
/
httple.js
executable file
·52 lines (41 loc) · 1.59 KB
/
httple.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
const http = require('http');
const badRequest = (res, message) => {
res.writeHead(400, message);
res.end();
}
const reqJsonToServerFunc = (jsonStr, validationObj, validationKeys, validationKeysStr, req, res, serverFunc) => {
let jsonObj;
try {
jsonObj = JSON.parse(jsonStr);
} catch {
return badRequest(res, "Invalid json");
}
const jsonObjKeys = Object.keys(jsonObj).sort();
const jsonObjKeysStr = JSON.stringify(jsonObjKeys);
if (jsonObjKeysStr !== validationKeysStr) return badRequest(res, "Invalid json key");
validationKeys.forEach(key => {
if (!validationObj[key](jsonObj[key])) return badRequest(res, `Invalid request json value. key: ${key}`);
})
serverFunc(req, res, jsonObj);
}
const httple = (endpointMethod, portNumber, allowEndpointRegexp, validationObj, serverFunc, callbackFunc) => {
endpointMethod = endpointMethod.toUpperCase();
let validationKeys;
let validationKeysStr;
if (!!validationObj) {
validationKeys = Object.keys(validationObj).sort();
validationKeysStr = JSON.stringify(validationKeys);
}
http.createServer((req, res) => {
if (endpointMethod !== req.method) return badRequest(res, "Invalid method");
if (!allowEndpointRegexp.test(req.url)) return badRequest(res, "Invalid endpoint");
if (!validationObj) {
serverFunc(req, res);
return;
}
req.on('data', (jsonStr) =>
reqJsonToServerFunc(jsonStr, validationObj, validationKeys, validationKeysStr, req, res, serverFunc)
);
}).listen(portNumber, callbackFunc);
}
module.exports = httple;