javascript为函数参数添加一个值

时间:2011-08-29 23:55:46

标签: javascript node.js arguments express

我正在尝试将一个参数添加到作为快速js路由中的参数传递的函数中。

这是一条快递路线:

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

函数someFunc就在那里接受参数req,res,next。

我想为它添加一个额外的参数。如果我使用apply或call它似乎替换了所有现有的参数。

我希望能够做到:

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

我该怎么做?感谢。

2 个答案:

答案 0 :(得分:0)

我不确定这是最好的方法,但你可以这样做:

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');
});

答案 1 :(得分:0)

我会创建一条这样的路线:

// 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 }));

这样您的路线设置就没有任何逻辑。

相关问题