-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpjsonapi.js
More file actions
36 lines (35 loc) · 867 Bytes
/
Copy pathhttpjsonapi.js
File metadata and controls
36 lines (35 loc) · 867 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
32
33
34
35
36
var http=require('http');
var url=require('url');
//create the server using the callback
var server=http.createServer(function(req,res)
{
var urlobj=url.parse(req.url,true);
if (urlobj.pathname=='/api/parsetime')
{
res.writeHead(200,{'content-type':'text/json'});
var iso=urlobj.query.iso;
var dt=new Date(iso);
var jsn={};
jsn.hour=dt.getHours();
jsn.minute=dt.getMinutes();
jsn.second=dt.getSeconds();
res.end(JSON.stringify(jsn));
}
else
if (urlobj.pathname=='/api/unixtime')
{
res.writeHead(200,{'content-type':'text/json'});
var iso=urlobj.query.iso;
// console.log(iso);
var dt=new Date(iso);
var jsn={};
jsn.unixtime=dt.getTime();
res.end(JSON.stringify(jsn));
}
else
{
res.writeHead(404);
res.end();
}
});
server.listen(process.argv[2]);