-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
42 lines (34 loc) · 1.15 KB
/
main.js
File metadata and controls
42 lines (34 loc) · 1.15 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
const http = require('http');
const os = require('os');
const readBody = async (req) => new Promise((resolve, reject) => {
const data = [];
req
.on('data', (chunk) => data.push(chunk))
.on('error', (err) => reject(err))
.on('end', () => resolve(Buffer.concat(data).toString()));
});
const log = (...args) => console.log(new Date().toISOString(), ...args);
const server = http.createServer(async (req, res) => {
try {
const info = {
method: req.method,
url: req.url,
headers: req.headers,
host: os.hostname(),
workspaceId: parseInt(process.env['WORKSPACE_ID']),
};
if (req.method !== 'GET') {
info.body = await readBody(req);
}
log(JSON.stringify(info));
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify(info, null, ' '));
} catch (e) {
log(e);
res.writeHead(500, {'Content-Type': 'application/json'});
res.write(JSON.stringify({error: e.message}));
} finally {
res.end();
}
});
server.listen(process.env['PORT'] ?? 3000);