• NodeJS之express的路由浅析


    路由路径和请求方法一起定义了请求的端点,它可以是字符串、字符串模式或者正则表达式。后端在获取路由后,可通过一系列类似中间件的函数去执行事务。

    可使用字符串的路由路径:

    // 匹配根路径的请求
    app.get('/', function (req, res) {
      res.send('root');
    });
    
    // 匹配 /about 路径的请求
    app.get('/about', function (req, res) {
      res.send('about');
    });
    
    // 匹配 /random.text 路径的请求
    app.get('/random.text', function (req, res) {
      res.send('random.text');
    });

    可使用字符串模式的路由路径:

    // 匹配 acd 和 abcd
    app.get('/ab?cd', function(req, res) {
      res.send('ab?cd');
    });
    
    // 匹配 /abe 和 /abcde
    app.get('/ab(cd)?e', function(req, res) {
     res.send('ab(cd)?e');
    });

    可使用正则表达式的路由路径:

    // 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
    app.get(/.*fly$/, function(req, res) {
      res.send('/.*fly$/');
    });

    可以为请求处理提供多个回调函数,其行为类似中间件。唯一的区别是这些回调函数有可能调用 next('route') 方法而略过其他路由回调函数。可以利用该机制为路由定义前提条件,如果在现有路径上继续执行没有意义,则可将控制权交给剩下的路径。

    路由句柄有多种形式,可以是一个函数、一个函数数组,或者是两者混合,如下所示

    使用一个回调函数处理路由:

    app.get('/example', function (req, res) {
      res.send('Hello from A!');
    });

    使用多个回调函数处理路由(记得指定 next 对象):

    app.get('/example/b', function (req, res, next) {
      console.log('response will be sent by the next function ...');
      next();
    }, function (req, res) {
      res.send('Hello from B!');
    });

    使用回调函数数组处理路由:

    var cb0 = function (req, res, next) {
      console.log('CB0');
      next();
    }
    
    var cb1 = function (req, res, next) {
      console.log('CB1');
      next();
    }
    
    var cb2 = function (req, res) {
      res.send('Hello from C!');
    }
    
    app.get('/example/c', [cb0, cb1, cb2]);

    混合使用函数和函数数组处理路由:

    var cb0 = function (req, res, next) {
      console.log('CB0');
      next();
    }
    
    var cb1 = function (req, res, next) {
      console.log('CB1');
      next();
    }
    
    app.get('/example/d', [cb0, cb1], function (req, res, next) {
      console.log('response will be sent by the next function ...');
      next();
    }, function (req, res) {
      res.send('Hello from D!');
    });

    可使用 express.Router 类创建模块化、可挂载的路由句柄。Router 实例是一个完整的中间件和路由系统。

    var log=require("./log/log");
    var express=require("express");
    var app=express();
    
    //首先定义一个路由
    var router=express.Router();
    
    //使用中间件
    router.use(function firstFunc(req,res,next){
        console.log("Time: ",Date.now());
        next();
    });
    
    //如果是根目录
    router.get("/",function(req,res){
        res.send("home page");
    });
    
    //如果是根目录下的about目录
    router.get("/about",function(req,res){
        res.send("about page");
    });
    
    //使用路由
    //可输入http://127.0.0.1:8000/myRoot得到home page
    //可输入http://127.0.0.1:8000/myRoot/about得到about page
    //在得到返回的page之前,会先执行firstFunc函数
    app.use("/myRoot",router);
    
    //开启站点
    app.listen(8000,function(){
        log.info("站点开启")
    });

    通过路由,可以在返回页面之前,先通过中间件执行若干事物,而且这些中间件是当前路由共享的,这样可以省去许多重复代码,增加代码可利用率的有效性。还可以将Router注册为模块导出,代码更加有可读性。

    注明:以上学习内容来自:http://www.expressjs.com.cn/guide/routing.html

  • 相关阅读:
    Asp.MVC 各个版本比较(资源整合)与WebForm的区别
    20款最新且极具创意的jQuery插件(附下载)
    php java net 开发比较
    移动互联网渠道乱象
    基于微软IIS/.NET平台开发的知名网站 (补充)
    sqlsql语句查询优化总结,建议及写法技巧(汇总)
    总结关于对日外包的一些想法
    .net跨平台解决方案mono真正实现C#代码一次编写处处运行(微软已经正式支持夸平台框架aspnet core)
    .NET 常用经典学习资源网站推荐
    常用visual studio 插件工具
  • 原文地址:https://www.cnblogs.com/williamwsj/p/6144856.html
Copyright © 2020-2023  润新知