nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.78k stars 7.64k forks source link

CORS Preflight requests fail with 404 #9291

Closed nils-jansen closed 2 years ago

nils-jansen commented 2 years ago

Is there an existing issue for this?

Current behavior

When sending a PATCH request from the frontend (localhost:3000) to my backend (localhost:5000), the browser sends a preflight OPTIONS request to the backend which fails with 404: image image

In main.ts:

const app = await NestFactory.create(AppModule);
app.enableCors();

Minimum reproduction code

n/a

Steps to reproduce

I can't reproduce this deterministically as it happens only sporadically :(

Expected behavior

No CORS Error in browser

Package

Other package

No response

NestJS version

8.0.0

Packages versions

"@nestjs/axios": "^0.0.5",
"@nestjs/common": "^8.0.0",
"@nestjs/config": "^1.2.0",
"@nestjs/core": "^8.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/mongoose": "^9.0.2",
"@nestjs/passport": "^8.2.1",
"@nestjs/platform-express": "^8.0.0",

Node.js version

16.13

In which operating systems have you tested?

Other

This might not be a bug with NestJS, I just don't know how to debug this properly. I can't provide a useful reproduction because the issue only appears sporadically - one day it works, the next day it doesn't. I must be missing something. I've tried multiple browsers, same issue everywhere. I've deleted the dist/ folder before starting the server, didn't help.

nils-jansen commented 2 years ago

It just worked again, no changes to backend or frontend: image

micalevisk commented 2 years ago

btw when you call enableCors, NestJS just attaches the cors() middleware. So you should report on their repo

https://github.com/nestjs/nest/blob/52381b0fba39ecf64bda5bae4c2cf8fc348130e7/packages/platform-express/adapters/express-adapter.ts#L139-L141

nils-jansen commented 2 years ago

That would just be a guess though. Could it be that the middleware is somehow not attached by error? What would be the best way to debug this?

micalevisk commented 2 years ago

Could it be that the middleware is somehow not attached by error?

I'd say: probably not as that this.use(cors(options)) will invoke expressAppInstance.use(cors(options)) directly, so there's no nestjs-specific stuff here

Thus I believe you could rely on expressjs debugging tips to debug that (you can access the underlying expressAppInstance via app.getHttpAdapter().getInstance()). I've been using app.enableCors({ origin }) with no worries so far

It just worked again, no changes to backend or frontend

then you problably was running an old version of your backend (or there is some bug on nestjs side, of course)

nils-jansen commented 2 years ago

I debugged Express directly (https://expressjs.com/en/guide/debugging.html) and found out that the browser does not make any request at all, event though it is showing me a 404 with response headers and body in the dev console. Spooky stuff, will investigate further. Thanks for your help @micalevisk !