-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_json_api_server.js
35 lines (34 loc) · 1.19 KB
/
http_json_api_server.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
var http = require('http');
var url = require('url');
var server = http.createServer(function (req, res) {
if (req.method == 'GET') {
var urlParsed = url.parse(req.url);
if (urlParsed['pathname'] == '/api/parsetime') {
res.writeHead(200, {'Content-Type': 'application/json'});
console.log(urlParsed);
var iso = (urlParsed['query']).split('=')[1];
var date = new Date(iso);
var output = {
'hour': date.getHours(),
'minute': date.getMinutes(),
'second': date.getSeconds()
}
res.end(JSON.stringify(output));
}
else if (urlParsed['pathname'] == '/api/unixtime') {
res.writeHead(200, {'Content-Type': 'application/json'});
var iso = (urlParsed['query']).split('=')[1];
var date = new Date(iso);
var output = {
'unixtime': date.getTime(),
}
res.end(JSON.stringify(output))
}
else {
res.end('Invalid path');
}
} else {
res.end('Invalid method');
}
});
server.listen(process.argv[2]);