【问题标题】:Express (bodyParser) urlencoded hangs, without callback or throwing error, when running on Azure Function在 Azure Function 上运行时,Express (bodyParser) urlencoded 挂起,没有回调或抛出错误
【发布时间】:2021-09-05 01:55:30
【问题描述】:

我正在使用无服务器 Azure Functions 运行一个网站,但我发现当我在我的 Azure 函数(部署时)上提交 POST 请求时,请求挂起,没有任何日志。

这是我最初的 body-parser 配置:

app.use(express.json({limit: '500kb'}));
app.use(express.urlencoded({ extended: true, parameterLimit: 5000, limit: '500kb' }));

我发现它在urlencoded 中间件函数期间挂起。为了诊断这个问题,我添加了一大堆日志和错误捕获,但即便如此,它还是挂起。我还尝试将express.urlencoded 更改为bodyParser.urlencoded,以及删除parameterLimitlimit 选项。

当前正文解析器配置:

app.use((req, res, next) => {
    logger.info('=====json-start');
    express.json({limit: '500kb'})(req, res, (err) => {
        logger.info('=====json-end');
        if (err) {
            logger.error(err);
            next(err);
        }
        else next();
    });
});
app.use((req, res, next) => {
    logger.info('=====urlencoded-start');
    try {
        var func = bodyParser.urlencoded({ extended: true, /*parameterLimit: 5000, limit: '500kb'*/ verify: (req, res, buf) => {
            logger.info('this code is becoming a huge mess');
            logger.info(buf);
        } });
        logger.info(func);
        func(req, res, (err) => {
            logger.info('=====urlencoded-end');
            if (err) {
                logger.error(err);
                next(err);
            }
            else next();
        });
    }
    catch (err) {
        logger.fatal(err);
    }
});

Azure 函数输出:

2021-09-04T16:32:06.791 [Information] Executed '
2021-09-04T16:32:06.791 [Information] Executed 'Functions.primary' (Succeeded, Id=3ca82952-a459-4230-b884-e870c487b410, Duration=1600ms)
2021-09-04T16:32:11.983 [Information] Executing 'Functions.primary' (Reason='This function was programmatically called via the host APIs.', Id=cf302d42-32d8-4926-bae5-4638da39c79e)
2021-09-04T16:32:12.023 [Information] [INFO] default. - =========I am running- =====json-start
2021-09-04T16:32:12.024 [Information] [INFO] default. - =====json-end
2021-09-04T16:32:12.030 [Information] [INFO] default. - =====urlencoded-start
2021-09-04T16:32:12.030 [Information] [INFO] default. - [Function: urlencodedParser]formation] [INFO] default. - =====urlencoded-start
2021-09-04T16:32:12.030 [Information] [INFO] default. - [Function: urlencodedParser]

请注意,没有 [ERROR] 日志,这意味着 urlencoded catch 块和回调都没有运行。

预期输出(本地运行时):

[12:38:11] [LOCAL|dev] [INFO] default. - =====json-start
[12:38:11] [LOCAL|dev] [INFO] default. - =====json-end
[12:38:11] [LOCAL|dev] [INFO] default. - =====urlencoded-start
[12:38:11] [LOCAL|dev] [INFO] default. - [Function: urlencodedParser]
[12:38:11] [LOCAL|dev] [INFO] default. - Got url
[12:38:11] [LOCAL|dev] [INFO] default. - this code is becoming a huge mess
[12:38:11] [LOCAL|dev] [INFO] default. - <Buffer 65 6d 61 69 6c 3d 64 66 26 70 61 73 73 77 6f 72 64 3d 66 66 6f 6f 6f>
[12:38:11] [LOCAL|dev] [INFO] default. - =====urlencoded-end

有谁知道为什么在 Azure 函数上运行时会发生这种情况,而在本地运行时却没有?

【问题讨论】:

    标签: node.js azure express azure-functions body-parser


    【解决方案1】:

    我发现问题出在我使用的 Azure-Function 处理程序包azure-function-express。它没有为 Express 提供正确的身体信息。我切换到azure-aws-serverless-express,现在可以正常使用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多