zoukankan      html  css  js  c++  java
  • express API解析

    1.router.param([name,] callback)

     添加一个请求路径中指定参数触发的回调函数。name是参数名字或者是参数名字的数组,函数是对应的回调函数。回调函数的参数依次是req,res和中间件函数next,和对应参数的值。

     如果name是一个数组。回调函数依次被数组中的参数触发。更进一步,除了最后一个参数之外,回调函数到下一个回调函数之间都会调用next。

     对于最后一个参数,目前正在处理的函数调用next进入下一个中间件,就和name是字符串一样。

     例如::user获取到了。你可以把user自动映射到req.user上,它并入到路径中。或执行验证的参数输入。

    router.param('user', function(req, res, next, id) {
      // try to get the user details from the User model and attach it to the request object
      User.find(id, function(err, user) {
        if (err) {
          next(err);
        } else if (user) {
          req.user = user;
          next();
        } else {
          next(new Error('failed to load user'));
        }
      });
    });

    该回调函数是本地路由上定义它们的,它不能通过挂载app或者被router继承。因此,在router上定义的参数回调函数只能通过在router中routers上定义的参数被触发(app.use和app.router是隔离的两种处理模式,主要表现在layer.router的值上)。

    一个参数回调函数在请求和响应的过程中只能触发一次,即使是这个参数被匹配在多个路径上,例如下面的例子:

    router.param('id', function (req, res, next, id) {
      console.log('CALLED ONLY ONCE');
      next();
    })
    
    app.get('/user/:id', function (req, res, next) {
      console.log('although this matches');
      next();
    });
    
    app.get('/user/:id', function (req, res) {
      console.log('and this matches too');
      res.end();
    });

    请求为: GET /user/4

    结果是:

    CALLED ONLY ONCE
    although this matches
    and this matches too
    router.param(['id', 'page'], function (req, res, next, value) {
      console.log('CALLED ONLY ONCE with', value);
      next();
    })
    
    app.get('/user/:id/:page', function (req, res, next) {
      console.log('although this matches');
      next();
    });
    
    app.get('/user/:id/:page', function (req, res) {
      console.log('and this matches too');
      res.end();
    })

    请求是: /user/42/3

    结果是:

    CALLED ONLY ONCE with 42
    CALLED ONLY ONCE with 3
    although this matches
    and this matches too

     2.req.baseUrl

      一个router实例设置的URL路径,也就是只有app.use挂载的路径叫baseUrl。

    var greet = express.Router();
    greet.get('/jp', function (req, res) {
      console.log(req.baseUrl); // /greet
      res.send('Konichiwa!');
    });
    app.use('/greet', greet); // load the router on '/greet'

    即使你对加载router使用一个路径模式或者一组路径模式,baseUrl属性返回一个匹配的字符串,而不是路径。下面的例子是,greet被加载有两个路径模式。

    app.use(['/gre+t', '/hel{2}o'], greet); // load the router on '/gre+t' and '/hel{2}o'

    当一个请求是/greet/ip,那个req.baseUrl是/greet.当请求是/hello/jp,那req,baseUrl是/hello。 

    req.baseUrl类似于app对象的mountpath属性,除了 app.mountpath返回的一个路径的匹配模式。

    具体可以参考:https://segmentfault.com/q/1010000007085050/a-1020000007085142

    3.router.route(path)

    返回一个router的实例,这个实例能够调用HTTP的请求方法来处理中间件,如(get,post,head,等等),它能避免重复使用相同名字的router而报错。

    例如:

    var router = express.Router();
    
    router.param('user_id', function(req, res, next, id) {
      // sample user, would actually fetch from DB, etc...
      req.user = {
        id: id,
        name: 'TJ'
      };
      next();
    });
    
    router.route('/users/:user_id')
    .all(function(req, res, next) {
      // runs for all HTTP verbs first
      // think of it as route specific middleware!
      next();
    })
    .get(function(req, res, next) {
      res.json(req.user);
    })
    .put(function(req, res, next) {
      // just an example of maybe updating the user
      req.user.name = req.params.name;
      // save user ... etc
      res.json(req.user);
    })
    .post(function(req, res, next) {
      next(new Error('not implemented'));
    })
    .delete(function(req, res, next) {
      next(new Error('not implemented'));
    })

     4.sendFile

    module.exports = function(req, res, opt) {
        var applyNo = req.query.applyNo;
        console.log("applyNo:"+applyNo);
        var file = path.resolve(__dirname, "../../web/repay_jld.html");
        res.sendFile(file);
    }
    //页面跳转到repay_jld.html
    //如果请求的url中有applyNo参数,那么repay_jld.html
    //也会带有applyNo参数
  • 相关阅读:
    手撕面试官系列(十一):BAT面试必备之常问85题
    手撕面试官系列(十):面试必备之常问Dubbo29题+MySQL55题
    手撕面试官系列(九):分布式限流面试专题 Nginx+zookeeper
    手撕面试官系列(八):分布式通讯ActiveMQ+RabbitMQ+Kafka面试专题
    手撕面试官系列(七):面试必备之常问并发编程高级面试专题
    手撕面试官系列(六):并发+Netty+JVM+Linux面试专题
    手撕面试官系列(五):Tomcat+Mysql+设计模式面试专题
    手撕面试官系列(四 ):MongoDB+Redis 面试专题
    手撕面试官系列(三):微服务架构Dubbo+Spring Boot+Spring Cloud
    linux 使用socket代理
  • 原文地址:https://www.cnblogs.com/liuyinlei/p/6550306.html
Copyright © 2011-2022 走看看