-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (44 loc) · 1.25 KB
/
server.js
File metadata and controls
49 lines (44 loc) · 1.25 KB
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
'use strict';
const http = require('node:http');
const fs = require('node:fs');
require('./schema.js').load('./schema/');
const api = require('./api.js').load('./api/');
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);
};
const httpError = (res, status, message) => {
res.statusCode = status;
res.end(`"${message}"`);
};
http
.createServer(async (req, res) => {
const url = req.url === '/' ? '/index.html' : req.url;
const [first, second] = url.substring(1).split('/');
if (first === 'api') {
const method = api.get(second);
const args = await receiveArgs(req);
try {
const result = await method(...args);
if (!result) {
httpError(res, 500, 'Server error');
return;
}
res.end(JSON.stringify(result));
} catch (err) {
console.dir({ err });
httpError(res, 500, 'Server error');
}
} else {
const path = `./static/${first}`;
try {
const data = await fs.promises.readFile(path);
res.end(data);
} catch {
httpError(res, 404, 'File is not found');
}
}
})
.listen(8000);