javascript - Empty params object in express.js middleware - Stack Overflow

const app = express();var router = require('express').Router({mergeParams: true});const pay

const app = express();
var router = require('express').Router({mergeParams: true});
const payloadMiddleware = (req, resp, next) => {
  console.log('A:',req.params);
  const {params, query} = req;
  const payload = req.body;
  req.my_params = { params, query, payload };
  next();
};

router.use(payloadMiddleware);

router.get('/inventory/:itemID/price', async function GetPrice(req, res, next) {
  console.log('B', req.my_params);
  console.log('C', req.params);
}

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/', router);
server = app.listen(port);

GET /inventory/abc/price?a=1&b=2 yields

A: {} # unclear why this is empty
B: { params: {},
     query: { a: '1', b: '2' },
     payload: {} } 
C: {itemID: 'abc'} 

So I'm confused: I expect my middleware to find params in req, construct the new object, and assign it to req as my_params, then pass that to the inventory handler. And this is partially happening, in that the querystring is being parsed and then assigned in my middleware. Why is req.params === {} in my middleware function, but req.params = {itemID: 'abc'}in the GET handler itself?

There are no other routes with the same pattern, no other middleware...

Also no change is observed when I remove the options object from the second line, i.e. var router = require('express').Router();

const app = express();
var router = require('express').Router({mergeParams: true});
const payloadMiddleware = (req, resp, next) => {
  console.log('A:',req.params);
  const {params, query} = req;
  const payload = req.body;
  req.my_params = { params, query, payload };
  next();
};

router.use(payloadMiddleware);

router.get('/inventory/:itemID/price', async function GetPrice(req, res, next) {
  console.log('B', req.my_params);
  console.log('C', req.params);
}

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/', router);
server = app.listen(port);

GET /inventory/abc/price?a=1&b=2 yields

A: {} # unclear why this is empty
B: { params: {},
     query: { a: '1', b: '2' },
     payload: {} } 
C: {itemID: 'abc'} 

So I'm confused: I expect my middleware to find params in req, construct the new object, and assign it to req as my_params, then pass that to the inventory handler. And this is partially happening, in that the querystring is being parsed and then assigned in my middleware. Why is req.params === {} in my middleware function, but req.params = {itemID: 'abc'}in the GET handler itself?

There are no other routes with the same pattern, no other middleware...

Also no change is observed when I remove the options object from the second line, i.e. var router = require('express').Router();

Share Improve this question edited Jul 15, 2016 at 20:50 Ben asked Jul 15, 2016 at 20:33 BenBen 5,0873 gold badges51 silver badges88 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

payloadMiddleware is a generic middleware that isn't declared for any specific URL pattern containing parameters (it matches any request).

Express also doesn't know that requests passed to the middleware will eventually end up in the handler for /inventory/:itemID/price.

If you want the middleware to receive the parameters, you have to specifically use it against routes that match a pattern:

app.use('/inventory/:itemID/price', payloadMiddleware);

Or as part of the route handler:

router.get('/inventory/:itemID/price', payloadMiddleware, async function ...);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743749291a4500597.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信