标签:
var http = require(‘http‘) var url = require(‘url‘) function parsetime (time) { return { hour: time.getHours(), minute: time.getMinutes(), second: time.getSeconds() } } function unixtime (time) { return { unixtime : time.getTime() } } var server = http.createServer(function (req, res) { var parsedUrl = url.parse(req.url, true) var time = new Date(parsedUrl.query.iso) var result if (/^\/api\/parsetime/.test(req.url)) result = parsetime(time) else if (/^\/api\/unixtime/.test(req.url)) result = unixtime(time) if (result) { res.writeHead(200, { ‘Content-Type‘: ‘application/json‘ }) res.end(JSON.stringify(result)) } else { res.writeHead(404) res.end() } }) server.listen(Number(process.argv[2]))
[freeCodeCamp] solution to HTTP JSON API SERVER passed!
标签:
原文地址:http://www.cnblogs.com/tianhangzhang/p/5607947.html