Nodejs路由可通过什么模块实现的

 2418

nodejs路由可通过url模块实现。url模块用于处理客户端请求过来的URL,nodejs中可通过url模块对url地址进行解析,从而实现路由的操作。


Nodejs路由可通过什么模块实现的


nodejs 的路由

路由指的就是我们要针对不同的URL有不同的处理方式。

URL模块作用:处理客户端请求过来的URL

nodejs可通过 url 模块对 url 地址进行解析, 实现路由的操作

  1. const http = requrie('http')
  2. const url = requrie('url')
  3. http.createServer((req, res) => {
  4.     //  对请求的路径进行解析
  5.     let pathname = url.parse(req.url).pathname;
  6.     if(pathname == '/'){
  7.         // 相关的处理 
  8.     }else if(pathname == '/login'){
  9.            // 相关的处理
  10.     }....
  11.  
  12. }).listen(3000, () => {
  13.     console.log('server runnning ....')
  14. })


本文网址:https://www.zztuku.com/detail-10262.html
站长图库 - Nodejs路由可通过什么模块实现的
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐