Last active
August 3, 2017 14:54
-
-
Save lilobase/0767ff6c2a69d3311981f4bd68400dc5 to your computer and use it in GitHub Desktop.
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
const http = require('http'); | |
const url = require('url'); | |
const server = http.createServer(({url: path}, res) => { | |
const {pathname, query} = url.parse(path); | |
const date = new Date(query.split('=')[1]); | |
const response = content => { | |
res.writeHead(200, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(content)); | |
}; | |
const notFound = () => { | |
res.writeHead(404); | |
res.end(); | |
}; | |
if (pathname === '/api/parsetime') | |
return response({ | |
hour: date.getHours(), | |
minute: date.getMinutes(), | |
second: date.getSeconds() | |
}); | |
if (pathname === '/api/unixtime') | |
return response({ | |
unixTime: date.getTime() | |
}); | |
return notFound(); | |
}); | |
server.listen(process.argv[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment