我正在尝试在我的应用中创建路线并访问控制器方法。
我的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;
我的testController
var testController = function(){
function getAll (req, res) {
res.send('123456')
}
return {
getAll: getAll
}
}
module.exports = testController;
我不确定如何确保123456
返回请求。任何人都可以帮我吗?非常感谢!
答案 0 :(得分:1)
试试这个
route.get('/getall', testController().getAll);