【问题标题】:How to export a local function within an Express router.get() function?如何在 Express router.get() 函数中导出本地函数?
【发布时间】:2021-01-24 23:22:27
【问题描述】:

我希望使用module.exports = { router, functionName } 导出本地函数,但目前我正在努力这样做。 我的代码如下所示 - 我想知道如何导出控制器“checkUserLoginCredentials”。

router.get('/', async function checkUserLoginCredentials(req, res) {
    //Controller code handling user login
});

我希望导出控制器以用于单元测试,但由于控制器是本地函数,我不能简单地将它放在 module.exports 中,然后使用 const { checkUserLoginCredentials } = require('path') 将其导入我的测试文件中。任何帮助将不胜感激,谢谢。

【问题讨论】:

  • router.get("/", checkUserLoginCredentials),然后在之前声明函数。

标签: javascript node.js express controller module.exports


【解决方案1】:

我建议将控制器移动到它自己的文件中,然后使用require("./path/to/controller") 在路由和单元测试中使用它。

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2017-04-18
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多