一、修改server.js
var http = require("http");var url = require("url"); //导入内置url模块function start(route){ function onRequest(request, response){ var pathname = url.parse(request.url).pathname; //提取url console.log("Request received."); console.log("Request for " + pathname + " received.") route(pathname) //路由 response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello,World,Node.js"); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started.")}exports.start = start
二、增加router.js
内容如下,
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)