Skip to content

Instantly share code, notes, and snippets.

@IPRIT
Created April 11, 2015 13:52
Show Gist options
  • Select an option

  • Save IPRIT/7c0c6c775358a1d1545b to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/7c0c6c775358a1d1545b to your computer and use it in GitHub Desktop.
var args = process.argv.slice(2);
var http = require('http');
var url = require('url');
var server = http.createServer(function(req, res) {
if (req.method != 'GET') {
res.end("Allows to use only GET HTTP request");
return;
}
var parsedUrl = url.parse(req.url, true),
date = new Date(parsedUrl.query.iso);
var result = {};
switch (parsedUrl.pathname) {
case '/api/parsetime':
result = {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
};
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.write(JSON.stringify(result));
break;
case '/api/unixtime':
result = {
unixtime: date.getTime()
};
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.write(JSON.stringify(result));
break;
default:
res.writeHead(404);
}
res.end();
});
server.listen(+args[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment