azure-func-http icon indicating copy to clipboard operation
azure-func-http copied to clipboard

CORS does not work with azure-func-http

Open divyavanmahajan opened this issue 3 years ago • 0 comments

I'm submitting an issue


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

In the Azure function handler, createApp with const app = await NestFactory.create(AppModule, { cors: true });

Expected behavior

When calling the Azure function running locally, a HTTP OPTIONS request to the /restapi - it should return the CORS headers.

Minimal reproduction of the problem with instructions

Running the NestJS server directly (in express mode) curl -vvv -X OPTIONS http://myserver:3333/restapi returns CORS headers

But when running under func start curl -vvv -X OPTIONS http://myserver:7071/restapi should return the CORS accept header response. No headers are returned.

What is the motivation / use case for changing the behavior?

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: 14
- Platform:  Mac
- Azure Function 4.0.0
- @nestjs/azure-func-http:^0.8.0
- @nestjs/platform-express:^8.0.0
- express: 4.17.1
Others:
Sharing investigation if it helps.

The default CORS handling in the express router - wraps the done with a check to handle OPTIONS
[email protected]/node_modules/express/lib/router/index.js
line 162
 // for options requests, respond with a default if nothing else responds
  if (req.method === 'OPTIONS') {
    done = wrap(done, function(old, err) {
      if (err || options.length === 0) return old(err);
      sendOptionsResponse(res, options, old);
    });
  }

This method is not called by the Azure HTTP handler - so the default CORS handling for OPTIONS does not get invoked.

divyavanmahajan avatar Feb 22 '22 16:02 divyavanmahajan