手动路由...
server.js
创建http服务器,解析pathname,传递给router处理。
var http = require("http"); var url = require("url"); function start(route) { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); route(pathname); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); } http.createServer(onRequest).listen(3000); console.log("Server has started."); } exports.start = start;
router.js
根据pathname,分发数据到响应页面处理
function route(pathname) { console.log("About to route a request for " + pathname); } exports.route = route;
index.js
var server = require("./server"); var router = require("./router"); server.start(router.route);