-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateServer.js
More file actions
28 lines (25 loc) · 938 Bytes
/
createServer.js
File metadata and controls
28 lines (25 loc) · 938 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
const http = require('http');
/**
* Create an HTTP server that listens on the given host and port.
* It responds to health checks on `/health` with status 200.
*/
const createServer = (host, port, timeout) => {
http.createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('OK');
} else {
setTimeout(() => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Server Response from port ${port}`);
}, timeout);
}
}).listen(port, host, () => {
console.log(`Server running at http://${host}:${port}`);
});
};
createServer('127.0.0.1', 8001, 1000);
createServer('127.0.0.1', 8002, 500);
createServer('127.0.0.1', 8003, 2000);
createServer('127.0.0.1', 8004, 100);
createServer('127.0.0.1', 8005, 20000);