forked from adunkman/http-echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (29 loc) · 948 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
var http = require("http");
var url = require("url");
var querystring = require("querystring");
var server = http.createServer();
var port = process.env.PORT || 8080;
server.on("request", function (request, response) {
var requestbody = "";
request.on('error', (err) => {
console.error(err);
}).on('data', (chunk) => {
requestbody = requestbody + chunk;
}).on('end', () => {
console.log(requestbody);
});
var uri = url.parse(request.url);
var qs = uri.query ? querystring.parse(uri.query) : {};
var status = qs.status || 200;
var contentType = qs.contentType || "text/plain";
var body = qs.body || "OK";
response.writeHead(status, {
"Content-Type": contentType,
"Content-Length": body.length
});
console.log(uri.pathname + " - HTTP " + status + " (" + contentType + "): " + body);
response.end(body);
});
server.listen(port, function () {
console.log("listening on port " + port);
});