-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (51 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
58 lines (51 loc) · 1.63 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
50
51
52
53
54
55
56
57
58
const http = require('http');
const os = require('os');
const { version } = require('./package.json');
function getIpAddresses() {
const interfaces = os.networkInterfaces();
return Object.values(interfaces)
.flat()
.filter(Boolean)
.filter(({ internal }) => !internal)
.map(({ address }) => address);
}
function esc(value) {
return String(value)
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
const server = http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', version }));
return;
}
const headers = Object.entries(req.headers)
.map(([name, value]) => `<li><strong>${esc(name)}:</strong> ${esc(value)}</li>`)
.join('');
const html = `<!doctype html>
<html>
<head><meta charset="utf-8"><title>Node test app</title></head>
<body>
<h1>Node.js test app</h1>
<dl>
<dt>Application version</dt><dd>${esc(version)}</dd>
<dt>Container hostname</dt><dd>${esc(os.hostname())}</dd>
<dt>Container IP address(es)</dt><dd>${esc(getIpAddresses().join(', ') || 'none')}</dd>
<dt>HTTP hostname</dt><dd>${esc(req.headers.host || '')}</dd>
<dt>URI requested</dt><dd>${esc(req.url)}</dd>
</dl>
<h2>HTTP request headers</h2>
<ul>${headers}</ul>
</body>
</html>`;
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(html);
});
const port = Number(process.env.PORT || 8080);
server.listen(port, '0.0.0.0', () => {
console.log(`Listening on 0.0.0.0:${port}`);
});