37 Replies
What is
corsHeaders
defined as?
Also, you are only showing the error response. What is the successful response?
FInally, it is the OPTIONS
method that is failing. Does defining POST
automatically handle OPTIONS
?I don't understand what expected answer you want to get
Where can I check it?
I think you find something you asked for
I tried
export async function OPTIONS(req: NextRequest, res: NextResponse) {
return NextResponse.json({ res }, { headers: corsHeaders })
After doing a little reading, I find nothing that suggests that the
cors
middleware will work like you are using it.
There's a nextjs-cors
package that seems to adapt the Express cors
middleware to work with Next.js, though it's relatively old.
Were I you, I'd try creating a middleware.ts
and populating it as described here (though you can statically define the CORS options rather than pulling everything in from the environment to make it simpler).Medium
3 Ways To Configure CORS for NextJS 13 App Router API Route Handlers
Configure Cross-Origin Resource Sharing For NextJS 13 App Router
GitHub
GitHub - yonycalsin/nextjs-cors: :tada: nextjs-cors is a node.js pa...
:tada: nextjs-cors is a node.js package to provide a Connect/Express middleware that can be used to enable CORS with various options :rocket: - yonycalsin/nextjs-cors
It doesn't work for me as well
My middleware.ts looks like as in medium post
I have middleware.ts in project I'm making request to
Your server is on port 3001.
yes
I'm doing request from 3000 to 3001
There's no CORS headers though, according to the error.
Did you define the appropriate environment variables in a
.env
file?what?
I have .env.local file but how its related to CORS?
In project 1 (3000) I'm doing request like this
If you look at the
corsOptions
variable, it is pulling its configuration from the environment.I think its wrong
I want to allow multiple things
You have to include
OPTIONS
.Ok
Also I think that its worth to mention that I don't have
I simply don't know what should I put in there
Like that?
Even if I copy pasted - still same error
Have you restarted the dev server? (Don't know if this will have an effect, but it never hurts.)
I tried to clear .next and restart
pnpm dev
on project 1 (3000)Well, if anything, you need to restart the server setting the CORS headers, so the one on
3001
.ok now I deleted .next and restarted project 2 (3001) using
pnpm dev
And see new errorWhat is the HTTP status? (From the Network tab in the developer's tools.)
IDK
seems http
It looks like your request is finally setting the headers, but the request is failing.
Here is my API route on project 2 (3001)
api/test/route.ts
You're not defining
OPTIONS
, so it says HTTP 405: Method Not Allowed
.How to define OPTIONS?
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE","OPTIONS"],
You mean this?
Still same
Nope, you're defining a
POST
handler, but not OPTIONS
.Like this?
Same
The middleware should obviate the need for
NextCors
.Also I got error on server
May I call you in discord?
Sure.
There's a #voice-chat channel here we could use.
Solution
project 2 (3001)
api/test/route.ts
project 2 (3001)
middleware.ts
project 2 (3001)
.env.local
I'm doing request from project 1 (3000) to project 2 (3001) like this
Today I got this error - how to fix that?
Disable proxy
In nextjs, rewrite and middleware could solve it