-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathhttp.js
48 lines (41 loc) · 1.46 KB
/
http.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
'use strict';
const http = require('node:http');
const answerNotFound = (res, headers) => {
res.writeHead(404, headers);
res.end('Not found');
};
const receiveArgs = async (req) => {
const buffers = [];
for await (const chunk of req) buffers.push(chunk);
const data = Buffer.concat(buffers).toString();
return JSON.parse(data);
};
module.exports = (routing, port, console) => {
http
.createServer(async (req, res) => {
const headers = {
'Access-Control-Allow-Origin':
'*' /* @dev First, read about security */,
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Max-Age': 2592000, // 30 days
/** add other headers as per requirement */
};
const { url, socket } = req;
const [name, method, id] = url.substring(1).split('/');
const entity = routing[name];
if (!entity) return void answerNotFound(res, headers);
const handler = entity[method];
if (!handler) return void answerNotFound(res, headers);
res.writeHead(200, headers);
const src = handler.toString();
const signature = src.substring(0, src.indexOf(')'));
const args = [];
if (signature.includes('(id')) args.push(id);
args.push(...(await receiveArgs(req)));
console.log(`${socket.remoteAddress} ${req.method} ${url}`);
const result = await handler(...args);
res.end(JSON.stringify(result.rows));
})
.listen(port);
console.log(`API on port ${port}`);
};