-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (62 loc) · 2.22 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
63
64
65
66
67
68
69
70
71
var http = require("http");
var url = require("url");
var querystring = require("querystring");
//var server = http.createServer();
var port = process.env.PORT || 3000;
http.createServer((request, response) => {
const { headers, method, url } = request;
let body = [];
request.on('error', (err) => {
console.error(err);
}).on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
try{
const obj = JSON.parse(body);
let currdate = new Date();
let date_text = "Request received date and time: " + currdate.getDate() + "/" + currdate.getMonth() + "/" + currdate.getFullYear() + " -- " + currdate.getHours() + ":" + currdate.getMinutes() + ":" + currdate.getSeconds() + "s"
console.log(date_text);
console.log("This is the request: "+body);
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.end();
}
catch(e){
console.log("Begin - BAD JSON Format.");
console.log(" ");
console.log(" ");
console.log(" ");
console.log(" ");
console.log("End - BAD JSON Format.");
response.writeHead(400, {
"Content-Type": "text/plain"
});
response.end();
}
/*var uri = url.parse(request.url);
var qs = uri.query ? querystring.parse(uri.query) : {};
var status = qs.status || 200;*/
// At this point, we have the headers, method, url and body, and can now
// do whatever we need to in order to respond to this request.
});
}).listen(port, function () {
console.log("listening on port " + port);
});
/*function (request, response) {
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 || "hello there!";
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);
//})