问题
//in routes.js (a rough attempt of what I want)
app.get('/test', myCtrl.test(req, res, next, 'type1'));
//in myCtrl.js
exports.test = function(req, res, next, type){
res.jsonp(type);
};
Like this it gives an error: ReferenceError: req is not defined
回答1:
Wrap that in an anonymous function else it will exec right away:
app.get('/test', function(req, res) {
myCtrl.test(req, res, next, 'type1');
})
来源:https://stackoverflow.com/questions/26220473/how-to-call-a-controller-with-express-routes-and-include-a-defined-parameter