[英]how to make a request to controller in node in my case?
I am trying to create a route in my app and access the controller method.我正在尝试在我的应用程序中创建一个路由并访问控制器方法。
I have something like these in my route.js
我的
route.js
有类似的东西
var express = require('express');
var route = express.Router();
var testController = require('../controllers/testController');
//express middleware
route.use(function(req, res, next){
next();
})
//getting testController.getAll is not a function error
route.get('/getall', testController.getAll());
//Route.get() requires callback functions but got a [object Undefined] error
route.get('/getall', testController.getAll);
module.exports = route;
My testController我的测试控制器
var testController = function(){
function getAll (req, res) {
res.send('123456')
}
return {
getAll: getAll
}
}
module.exports = testController;
I am not sure how to make sure 123456
get returned to the request.我不确定如何确保
123456
返回到请求中。 Can anyone help me about it?任何人都可以帮助我吗? Thanks a lot!
非常感谢!
试试这个route.get('/getall', testController().getAll);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.