-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathindex.js
40 lines (39 loc) · 1003 Bytes
/
index.js
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
'use strict';
const http = require('node:http');
const server = http
.createServer((req, res) => {
const now = new Date();
console.info(`[${now}] Requested by ${req.socket.remoteAddress}`);
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
switch (req.method) {
case 'GET':
res.write(`GET ${req.url}`);
break;
case 'POST':
res.write(`POST ${req.url}`);
let rawData = '';
req
.on('data', chunk => {
rawData += chunk;
})
.on('end', () => {
console.info(`[${now}] Data posted: ${rawData}`);
});
break;
default:
break;
}
res.end();
})
.on('error', e => {
console.error(`[${new Date()}] Server Error`, e);
})
.on('clientError', e => {
console.error(`[${new Date()}] Client Error`, e);
});
const port = 8000;
server.listen(port, () => {
console.info(`[${new Date()}] Listening on ${port}`);
});