Node.js get()需要回调函数,但得到了一个[object Undefined]

Node.js get()需要回调函数,但得到了一个[object Undefined],node.js,typescript,router,Node.js,Typescript,Router,我的应用程序是用带有typescript的nodejs。我试图分离路由,并引入接口和控制器来执行后面的逻辑 应用程序ts const countryRoutes = require('./routes/countryroute') app.use('/countries', countryRoutes) var countryuController = require('./../controller/country/countrycontroller') var express = requ

我的应用程序是用带有typescript的nodejs。我试图分离路由,并引入接口和控制器来执行后面的逻辑

应用程序ts

const countryRoutes = require('./routes/countryroute')
app.use('/countries', countryRoutes)
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()

router.get('/getValidCountry', countryController.validCountry)
module.exports = router
interface ICountryController {
    validCountry(req: any, res: any)
}
class CountryController {
    constructor() {}
    validCountry(req: any, res: any) {
        //application Logic here
    }
}

module.exports = CountryController
countryRoute.ts

const countryRoutes = require('./routes/countryroute')
app.use('/countries', countryRoutes)
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()

router.get('/getValidCountry', countryController.validCountry)
module.exports = router
interface ICountryController {
    validCountry(req: any, res: any)
}
class CountryController {
    constructor() {}
    validCountry(req: any, res: any) {
        //application Logic here
    }
}

module.exports = CountryController
i国家控制器.ts

const countryRoutes = require('./routes/countryroute')
app.use('/countries', countryRoutes)
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()

router.get('/getValidCountry', countryController.validCountry)
module.exports = router
interface ICountryController {
    validCountry(req: any, res: any)
}
class CountryController {
    constructor() {}
    validCountry(req: any, res: any) {
        //application Logic here
    }
}

module.exports = CountryController
CountryController.ts

const countryRoutes = require('./routes/countryroute')
app.use('/countries', countryRoutes)
var countryuController = require('./../controller/country/countrycontroller')
var express = require('express')
var router = express.Router()

router.get('/getValidCountry', countryController.validCountry)
module.exports = router
interface ICountryController {
    validCountry(req: any, res: any)
}
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]
我试图改变在控制器文件中写入方法的方式,但得到了相同的异常。我也尝试过其他问题的解决方案,但没有一个对我有效

关于如何在类文件中为编写函数的任何建议。获取要接受的函数。

尝试以下操作:

将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
尝试以下操作:

将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

您只需更改应用程序结构,如下所示:

只需创建一个名为routes.ts的文件,然后在其中定义函数

var countryuController = require('./../controller/country/countrycontroller');

module.export = (app) => {
  app.get("/getValidCountry" , countryController.validCountry);
}

并在你的app.ts中使用以下路线:

const app = express();
const routes = require("./routes");

routes(app);


通过这种方式,您也可以链接多个控制器:D

您只需更改应用程序结构,如下所示:

只需创建一个名为routes.ts的文件,然后在其中定义函数

var countryuController = require('./../controller/country/countrycontroller');

module.export = (app) => {
  app.get("/getValidCountry" , countryController.validCountry);
}

并在你的app.ts中使用以下路线:

const app = express();
const routes = require("./routes");

routes(app);


通过这种方式,您也可以链接多个控制器:D

实际上,我不想实现批准,我想调用controller函数作为第二个参数本身,因为有很多路由实际上不想实现批准,我想调用controller函数作为第二个参数本身,因为有很多路由。这是由于countryuController中的输入错误造成的吗?想知道你最后是怎么做到的。这是不是因为countryuController(额外U)中的输入错误造成的?我想听听你到底是怎么做到的。