【问题标题】:error middleware in azure-mobile-apps-nodeazure-mobile-apps-node 中的错误中间件
【发布时间】:2017-04-22 04:29:59
【问题描述】:

我想从 api 文件夹中捕获错误。

但是,除非使用标准快速路由 app.get(someroute,handler) 声明的任何处理程序触发的错误,否则快速错误处理中间件不会捕获任何错误

对此有何建议?

提前致谢。

app.js

mobile.api.import('./api');
mobile.tables.initialize()
    .then(function () {
        app.use(mobile);    // Register the Azure Mobile Apps middleware
        app.listen(process.env.PORT || 3000);   // Listen for requests
    })
    .then(function(){
        if (process.env.NODE_ENV == 'production'){
            ///handle errors on prod
            app.use(function(err, req, res, next) {
                console.log('error catch - here');
                res.statusCode = 500;
                res.end();
            });
        }
    });

api/testing.js

module.exports = {
    "get": function (req, res, next) {
        throw new Error('this is error');
    }
};

【问题讨论】:

    标签: javascript node.js azure error-handling azure-mobile-services


    【解决方案1】:

    你的理解是正确的。 Azure 移动应用的 Node.js SDK 使用 Express,因此它使用相同的语义。 (旁注 - 我们简要讨论了不实现自定义 API 工具。它确实支持 Azure 门户的门户内编辑功能。

    一个更好的问题(而不是这个论坛的问题,它是关于回答编程问题)是你想要发生什么?在 https://github.com/Azure/azure-mobile-apps-node 存储库上提交问题,以便我们进行讨论。

    【讨论】:

      【解决方案2】:

      尝试在mobile.api.import 之后添加mobile.use(function (err, req, res, next) { /* ... */ })。这将在 azure-mobile-apps 错误处理程序之前安装错误处理中间件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-07
        • 2017-09-02
        • 2021-11-21
        • 1970-01-01
        • 2017-04-25
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多