-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPJSONAPIServer.js
More file actions
39 lines (32 loc) · 1.03 KB
/
Copy pathHTTPJSONAPIServer.js
File metadata and controls
39 lines (32 loc) · 1.03 KB
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
var http = require('http');
var url = require('url');
var port = process.argv[2];
http.createServer(function(req, resp) {
if(req.method != 'GET'){
return resp.end('Send only GET requests\n');
}
var reqUrl = url.parse(req.url, true);
if(reqUrl.pathname.slice(0,4) != '/api'){
return resp.end('Invalid path\n');
}
var timeIn;
if(reqUrl.query.iso) {
timeIn = new Date(reqUrl.query.iso);
} else {
return resp.end('No iso query string supplied\n');
}
var timeOut = new Object();
if(reqUrl.pathname == '/api/parsetime') {
// ISO
timeOut.hour = timeIn.getHours();
timeOut.minute = timeIn.getMinutes();
timeOut.second = timeIn.getSeconds();
} else if(reqUrl.pathname == '/api/unixtime') {
// Unix
timeOut.unixtime = timeIn.getTime();
} else {
return resp.end('Invalid endpoint');
}
resp.writeHead(200, { 'Content-Type': 'application/json'});
resp.end(JSON.stringify(timeOut), null, '\t');
}).listen(port);