Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created November 16, 2015 02:26
Show Gist options
  • Save danielrobertson/0f865588122fc8913727 to your computer and use it in GitHub Desktop.
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
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