express app.all()和app.use()

2020-06-08  本文已影响0人  IamaStupid
  1. app.use(‘/a’)可以匹配路由‘/a’,'/a/b',但是 app.all('/a')只能识别'/a'
  2. use通常是用作挂载中间件的,调用顺序是书写顺序。
    all是路由中指代所有的请求方式,如all('/a'),能同时覆盖:get('/a') 、 post('/a')、 put('/a') 等
module.exports = (app) => {
  // 加载中间件
  app.use('/pre', function(req, res, next) {
    console.log('app.use....')
    res.locals.title = "首页 123456"
    res.locals.favIconPath = '/pre/favicon.ico'
    res.locals.header = {
      info: 'home header 123456'
    }
    res.locals.footer = {
      info: 'home footer 123456'
    }
    next()
  });
  app.get('/pre/', function(req, res) {
    console.log('app.get("/pre/",....')
    // render(xxx, {})
    // 第一个参数对应的就是views设置路径下的xxx.hbs
    // 第二个参数就是 res.locals对象
    res.render('home', {
        title: "首页12",
        header: {
          info: 'home header12'
        },
        personInfoList: [{
            name: "王炮儿(一拳超人)",
            age: 20
        }, {
            name: "炮姐(御坂美琴)",
            age: 15
        }]
    });
  });
  app.get('/pre/about', function(req, res) {
    console.log('app.get("/pre/about/",....')
    // render(xxx, {})
    // 第一个参数对应的就是views设置路径下的xxx.hbs
    // 第二个参数就是 res.locals对象
    res.render('about', {
        title: "about12",
        personInfoList: [{
            name: "about王炮儿(一拳超人)",
            age: 20
        }, {
            name: "about炮姐(御坂美琴)",
            age: 15
        }]
    });
  });
}
上一篇 下一篇

猜你喜欢

热点阅读