nodejs路由模块使用

发布时间:2020-07-18 03:21:47 作者:素颜猪
来源:网络 阅读:359
  1. 创建路由模块(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

nodejs路由模块使用

推荐阅读:
  1. Nodejs动态加载路由,Nodejs遍历目录,Nodejs路由工具
  2. nodejs常用模块以及使用示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

路由 nodejs 模块使用

上一篇:Hadoop基础原理

下一篇:Go中的工程管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》