开发者

javascript adding a value to function arguments

I'm trying to add an argument to a function passed along as an argument in an express js route.

This is an expressjs route:

app.get('path', some开发者_开发问答Func,
function(req, res, next) {
  res.render('layout');
});

The function someFunc right there takes the arguments req, res, next.

I want to add an additional argument to it. If I use apply or call it seems to replace all the existing arguments.

I want to be able to do:

someFunction (req, res, next, custom) {}

How can I do this? Thanks.


I am not sure that this is the best way but you could do something like this :

var someFunc = function (req, res, next, custom) { console.log(custom); next(); }

app.get('path', 
function (req, res, next) { someFunc(req, res, next, 'custom') },
function(req, res, next) {
    res.render('layout');
});


I would create a route like this:

// Inside your routes.js:
module.exports.someRoute = function(myArgument) {
  return function(req, res, next) {
    // Do whatever you want with myArgument.
  };
};

// Inside your app.js:
app.get('path', routes.someRoute({ foo: 1 }));

This way your route setup is clear of any logic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜