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