[freeCodeCamp] solution to HTTP JSON API SERVER passed!
Posted Jonas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[freeCodeCamp] solution to HTTP JSON API SERVER passed!相关的知识,希望对你有一定的参考价值。
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!的主要内容,如果未能解决你的问题,请参考以下文章
Arguments Optional FreeCodeCamp