[英]Express app.param() middleware fires multiple times per request
I've supplied my Express 4
app with some middleware to fire every time a particular set of parameters shows up in a request like so: 我为
Express 4
应用程序提供了一些中间件,以便每次在请求中显示一组特定的参数时都会触发该中间件,如下所示:
app.param(['mystic', 'donkey', 'toadstool'], function(req, res, next, param) {
console.log(req.params);
});
Unfortunately if I visit GET /:mystic/:donkey/:toadstool
. 不幸的是,如果我访问
GET /:mystic/:donkey/:toadstool
。 It prints out: 它输出:
{ mystic: x, donkey: y, toadstool: z }
{ mystic: x, donkey: y, toadstool: z }
{ mystic: x, donkey: y, toadstool: z }
When all I want is just: 当我想要的只是:
{ mystic: x, donkey: y, toadstool: z }
Is there a way to stop it from firing multiple times per req/response and still use an array at the first parameter to app.param
? 有没有一种方法可以阻止它每次请求/响应多次触发,并且仍然在
app.param
的第一个参数上使用数组?
if you see in the source code of app.param , you will realize that for every param in the array you put: ['mystic', 'donkey', 'toadstool']
, the callback you bind to app.param
it will be called sequentially, in this case three times. 如果您在app.param的源代码中看到 ,您将意识到,对于您放置的数组中的每个参数:
['mystic', 'donkey', 'toadstool']
,您绑定到app.param
的回调将是依次调用,在这种情况下为三次。
So there is no way you could do to have all the values in once invocation of app.param
, probably you could grab all your params one at a time, something like: 因此,您不可能一经调用
app.param
就拥有所有值,可能您一次可以获取所有参数,例如:
var express = require('express');
var app = express();
app.param('mystic', function(req, res, next, mystic) {
console.log('mystic: %s', mystic);
next();
});
app.param('donkey', function(req, res, next, donkey) {
console.log('donkey: %s', donkey);
next();
});
app.param('toadstool', function(req, res, next, toadstool) {
console.log('toadstool: %s', toadstool);
next();
});
app.get('/:mystic/:donkey/:toadstool', function(req, res) {
res.send('hey');
});
app.listen(4040, function() {
console.log('server up and running');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.