-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpJSONApiServer.js
More file actions
32 lines (28 loc) · 985 Bytes
/
HttpJSONApiServer.js
File metadata and controls
32 lines (28 loc) · 985 Bytes
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
const http = require("http");
const url = require("url");
http.createServer(function(request, response){
response.writeHead(200, { 'Content-Type': 'application/json ' });
var urlInfo = url.parse(request.url, true);
var dateTime = new Date(urlInfo.query["iso"]);
if(!urlInfo.query["iso"]){
response.writeHead(400);
response.end("Bad request: missing iso time");
return;
}
if(urlInfo.pathname === "/api/parsetime"){
var res = {
hour: dateTime.getHours(),
minute: dateTime.getMinutes(),
second: dateTime.getSeconds()
}
response.end(JSON.stringify(res));
} else if(urlInfo.pathname === "/api/unixtime"){
var res = {
unixtime: dateTime.getTime(),
}
response.end(JSON.stringify(res));
} else{
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("404 Not Found\n");
}
}).listen(+process.argv[2]);