-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (52 loc) · 1.48 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const http = require("http");
const port = 3000;
var process = require('process')
process.on('SIGINT', () => {
console.info("Interrupted")
process.exit(0)
});
process.on('SIGTERM', () => {
console.info("TERM")
process.exit(0)
});
http
.createServer((req, res) => {
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS, POST, GET, PUT",
"Access-Control-Max-Age": 2592000, // 30 days
"Access-Control-Allow-Headers": "*",
"Content-Type": "text/plain",
};
if (req.method === "OPTIONS") {
res.writeHead(204, headers);
res.end();
return;
}
if (["GET", "POST", "PUT"].indexOf(req.method) > -1) {
res.writeHead(200, headers);
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
res.write("(server latest)\n")
res.write("headers\n")
res.write("===============\n\n")
for (const header in req.headers) {
res.write(`${header}: ${req.headers[header]}\n`);
}
res.write("\n\n");
res.write("body\n")
res.write("===============\n\n")
res.write(body);
res.write("\n\n===============\nend.\n")
res.end();
});
return;
}
res.writeHead(405, headers);
res.end(`${req.method} is not allowed for the request.`);
})
.listen(port);
console.log("listening on port " + port);