(导出)如何在 Express 中访问控制器内的应用程序对象

Sta*_*tas 0 javascript node.js express ecmascript-6 next.js

我试图弄清楚如何从路由控制器访问应用程序对象。在我的路线文件中我有

const apiController = require('../controllers/mainController')
module.exports = (app) => {


    app.post("/stop",
        apiController.stopFlow
    );

    app.post("/specificSearch",
        apiController.initiateSearch);
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我无法访问(app)这些控制器内部的对象,但是,如果我执行类似的操作

module.exports = (app) =>{

    app.post('/stop', (req,res)=>{
        console.log(app)
    })

}
Run Code Online (Sandbox Code Playgroud)

然后一切正常,所以我很好奇有没有办法将它传递给我的apiController?我的apiController样子是这样的

module.exports = {

    async stopFlow(req, res) {
        console.log("Stop");

        console.log(app)

    },
}
Run Code Online (Sandbox Code Playgroud)

我能做什么来解决这个问题?

jfr*_*d00 5

在 Express 中,您可以req.app从任何请求处理程序内部使用来访问该app对象。文档中对此进行了解释。