Skip to content

Instantly share code, notes, and snippets.

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

  • Save IPRIT/23f42f03e242c9dc510b to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/23f42f03e242c9dc510b 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("Alows to use only GET HTTP request");
return;
}
var parsedUrl = url.parse(req.url, true),
date = new Date(parsedUrl.query.iso);
res.writeHead(200, {
'Content-Type': 'application/json'
});
var result = {};
switch (parsedUrl.pathname) {
case '/api/parsetime':
result = {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
};
res.write(JSON.stringify(result));
res.end();
break;
case '/api/unixtime':
result = {
unixtime: date.getTime()
};
res.write(JSON.stringify(result));
res.end();
break;
}
});
server.listen(+args[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment