node.jsazureexpressazure-functionsbody-parser

Express (bodyParser) urlencoded hangs, without callback or throwing error, when running on Azure Function


I am running a website using serverless Azure Functions, but I found that when I submit a POST request on my Azure function (while deployed) the request hangs without any logs.

This is what my body-parser config was originally:

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

I identified that it hangs during the urlencoded middleware function. To diagnose the issue, I added a crap-ton of logs and error catching, but even with that, it hangs. I've also tried changing express.urlencoded to bodyParser.urlencoded, as well as removing the parameterLimit and limit options.

Current body-parser config:

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 Function output:

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]

Note that there are no [ERROR] logs, meaning that neither the urlencoded catch block nor the callback are run.

Expected output (when running locally):

[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

Does anyone have any idea why this is happening when running on the Azure function, but not when running locally?


Solution

  • I found that the issue was the Azure-Function handler package I was using, azure-function-express. It wasn't providing Express with the proper body information. I switched to azure-aws-serverless-express and it works fine now.