/
http.js
executable file
·38 lines (35 loc) · 908 Bytes
/
http.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
#!/usr/bin/env node
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
const http = require('http');
const util = require('util');
const url = require('url');
http.createServer(
(req, res) => {
const myUrl = new url.URL(req.url, 'http://example.com');
const searchString = [];
for (const [key, value] of myUrl.searchParams) {
searchString.push(`<div>${key}: ${value}</div>\n`)
}
const reqString = [];
for (let prop of Object.keys(req).sort()) {
reqString.push(`<div>${prop}: ${util.inspect(req[prop])}</div>\n`);
}
ret = `<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>hello html</title>
</head>
<body>
<div>req.url: ${req.url}</div>
<div>url.pathname: ${myUrl.pathname}</div>
<div>url.search:</div>
${searchString.join('')}
<div>req:</div>
${reqString.join('')}
</body>
</html>
`
res.write(ret);
res.end();
}).listen(8080);