========================================ui
server.jsurl
var http = require("http");spa
var url = require("url");router
function start(route){//route.js 的route方法server
//服務實現it
function onRequest(request,response){io
var pathname = url.parse(request.url.).pathname;console
route(pathname); //function
response.writeHead(200,{"Content-Type":"text/plain"});後臺
response.write("只是一個練手罷了");
response.end();
}
//建立服務
http.createServer(onRequest).listen(8888);//端口號8888
console.log("服務啓動");//後臺輸出,表示服務啓動成功
}
exports.start = start; //將start方法暴露出去
========================================
route.js
function route(pathname){
console.log("請求路徑是:" + pathname); //後臺查看請求路勁 區分請求的地址
}
exports.route = route;
========================================
index.js
var server = require("./server");
var router = require("./route");
server.start(router.route);//傳遞給start的是router的route方法