Created
December 14, 2013 07:13
-
-
Save cuipengfei/7956421 to your computer and use it in GitHub Desktop.
learn you node http json api
This file contains 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
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