您好,登錄后才能下訂單哦!
創(chuàng)建路由模塊(route.js)
function route(pathname){
console.log("About to route a request for "+pathname);
}
exports.route = route;
創(chuàng)建http服務(wù)模塊(server.js)var http = require("http");var url = require("url");function start(route){ function onRequest(request,response){ var pathname = url.parse(request.url).pathname; if (pathname != "/favicon.ico") { 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(8888); console.log("Server has started");}exports.start = start;創(chuàng)建index.js來使用http服務(wù)器模塊和路由模塊var http = require("./server");var router = require("./route");http.start(router.route);執(zhí)行index.js并查看結(jié)果
執(zhí)行命令:node index.js
訪問如下地址:http://localhost:8888/demo
執(zhí)行結(jié)果:
Server has started
Request for /demo received
About to route a request for /demo
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。