how to call a controller with express routes and include a defined parameter

南笙酒味 提交于 2019-12-25 03:52:40

问题


//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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!