Skip to content

Instantly share code, notes, and snippets.

@cuipengfei
Created December 14, 2013 07:13
Show Gist options
  • Save cuipengfei/7956421 to your computer and use it in GitHub Desktop.
Save cuipengfei/7956421 to your computer and use it in GitHub Desktop.
learn you node http json api
var http = require('http')
var url = require('url')
var port = process.argv[2]
http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'application/json' })
var reqContent = url.parse(request.url, true)
var isoDate = new Date(reqContent.query.iso);
function time() {
return {
hour: isoDate.getHours(),
minute: isoDate.getMinutes(),
second: isoDate.getSeconds()
};
}
function unixTime() {
return {
unixtime: isoDate.getTime()
};
}
if (reqContent.pathname == "/api/parsetime") {
response.end(JSON.stringify(time()))
} else if (reqContent.pathname == "/api/unixtime") {
response.end(JSON.stringify(unixTime()))
}
}).listen(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment