Created
November 16, 2015 02:26
-
-
Save danielrobertson/0f865588122fc8913727 to your computer and use it in GitHub Desktop.
An HTTP server with two resources that accept date in ISO format and return HHMMSS or milliseconds in JSON format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var port = process.argv[2]; | |
http.createServer(function (request, response) { | |
urlInfo = url.parse(request.url, true); | |
iso = urlInfo.query['iso']; | |
response.writeHead(200, {'Content-Type': 'application/json'}); | |
if (urlInfo.pathname === '/api/parsetime') { | |
var date = new Date(iso); | |
var json = JSON.stringify({ | |
hour: date.getHours(), | |
minute: date.getMinutes(), | |
second: date.getSeconds() | |
}); | |
response.end(json) | |
} | |
if (urlInfo.pathname === '/api/unixtime') { | |
var date = new Date(iso); | |
var json = JSON.stringify({ | |
unixtime: date.getTime() | |
}); | |
response.end(json) | |
} | |
}).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment