-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (29 loc) · 1003 Bytes
/
index.js
File metadata and controls
35 lines (29 loc) · 1003 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
31
32
33
34
35
const http = require('http');
const { getConfig } = require('./config');
const methods = require('./methods');
const { parseJsonBody } = require('./utils');
const path = require('path');
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Request-Method': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Headers': '*',
};
const server = http.createServer(async (req, res) => {
const method = methods[req.method.toLowerCase()];
if (typeof method !== 'function') {
res.writeHead(400);
res.end('Wrong method');
}
const body = await parseJsonBody(req);
const data = await method(req.url, body);
res.setHeader("Content-Type", "application/json");
res.writeHead(200, headers);
res.end(JSON.stringify(data));
return;
});
server.listen(getConfig().port, () => {
console.log(`Data base is ${path.join(__dirname, getConfig().dbFilename)}`);
console.log('Server is working');
console.log(`http://localhost:${getConfig().port}/`);
});