溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nodejs路由模塊使用

發(fā)布時間:2020-07-18 03:21:47 來源:網(wǎng)絡(luò) 閱讀:367 作者:素顏豬 欄目:開發(fā)技術(shù)
  1. 創(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

nodejs路由模塊使用

向AI問一下細(xì)節(jié)

免責(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)容。

AI