我的应用程序在带有打字稿的nodejs中。我正在尝试隔离路由,并引入接口和控制器来执行后面的逻辑。
app.ts
const countryRoutes = require('./routes/countryroute')
app.use('/countries', countryRoutes)
countryRoute.ts
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()
router.get('/getValidCountry', countryController.validCountry)
module.exports = router
ICountryController.ts
interface ICountryController {
validCountry(req: any, res: any)
}
CountryController.ts
class CountryController {
constructor() {}
validCountry(req: any, res: any) {
//application Logic here
}
}
module.exports = CountryController
直到countryRoute.ts一切正常,但是此后控件没有转到countryController.ts,则出现以下错误
Route.get() requires a callback function but got a [object Undefined]
我尝试更改在控制器文件中编写方法的方式,但是遇到相同的异常。我也尝试过其他问题的解决方案,但没有一个对我有用。
关于如何在类文件中编写函数以供。get函数接受的任何建议。
尝试以下操作:
如下更改countryRoute.ts:
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()
router.get('/getValidCountry', function(req, res){
countryController.validCountry(req, res))
});
module.exports = router