-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13httpJSON.js
More file actions
31 lines (24 loc) · 937 Bytes
/
13httpJSON.js
File metadata and controls
31 lines (24 loc) · 937 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
var http = require('http');
var querystring = require('querystring');
var url = require('url');
var server = http.createServer(function(request, response) {
var urlparse = url.parse(request.url, true); // returns object
var myTimeStr = urlparse.query.iso; // returns string
var myTime = new Date(myTimeStr); // returns object
var json = JSON.stringify(timeParsed(myTime));
if(urlparse.pathname == '/api/parsetime'){
response.writeHead(200, {'Content-Type': 'application/json'})
response.end(JSON.stringify(timeParsed(myTime)));
}
if(urlparse.pathname == '/api/unixtime'){
response.writeHead(200, {'Content-Type': 'application/json'})
response.end(JSON.stringify(timeUnix(myTime)));
}
})
server.listen(process.argv[2]);
function timeParsed(date){
return {hour: date.getHours(), minute: date.getMinutes(), second: date.getSeconds()};
}
function timeUnix(date){
return {unixtime: date.getTime()};
}