如何使用快速路由调用控制器并包含已定义的参数 [英] how to call a controller with express routes and include a defined parameter

查看:55
本文介绍了如何使用快速路由调用控制器并包含已定义的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//在routes.js中(我想要的一个粗略尝试)

//in routes.js (a rough attempt of what I want)

app.get('/test', myCtrl.test(req, res, next, 'type1'));

//在myCtrl.js中

//in myCtrl.js

exports.test = function(req, res, next, type){
    res.jsonp(type);
};

就像这样,它给出一个错误:ReferenceError:未定义请求

Like this it gives an error: ReferenceError: req is not defined

推荐答案

将其包装在一个匿名函数中,否则它将立即执行:

Wrap that in an anonymous function else it will exec right away:

app.get('/test', function(req, res) {
    myCtrl.test(req, res, next, 'type1');
})

这篇关于如何使用快速路由调用控制器并包含已定义的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆