express router.param([name] callback)为什么name传数组多个param不行?

const express = require('express')
const router = express.Router()
const Student = require('../students')


router.param(['id','page'], function (req, res, next, value) {
    console.log('11', value);
    next();
  })

router.get('/user/:id/:page', function (req, res, next) {
    console.log('22');
    next();
});
  
router.get('/user/:id/:page', function (req, res) {
    console.log('33');
    res.end();
});

//浏览器  
http://localhost:3000/user/42/2323


//控制台结果
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
app is running at port 3000
22
33

//数组里面只有一个值,或者第一个参数直接传一个param

const express = require('express')
const router = express.Router()
const Student = require('../students')


router.param(['id'], function (req, res, next, value) {
    console.log('11', value);
    next();
  })

router.get('/user/:id', function (req, res, next) {
    console.log('22');
    next();
});
  
router.get('/user/:id', function (req, res) {
    console.log('33');
    res.end();
});

//浏览器  
http://localhost:3000/user/42


//控制台结果
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
app is running at port 3000
11 42
22
33

不知道为什么 按官方文档来的, 第一个参数传数组且多个param 就进不去,求大佬们帮助下

阅读 1.8k
1 个回答

最后发现原因是 router.param 不接受多参数的, 但是express中文文档没人维护 API没更新,引发的错误。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题