Next.js Discord

Discord Forum

cors on nextjs application router 14

Unanswered
Levriero Sardo posted this in #help-forum
Open in Discord
Avatar
Levriero SardoOP
I have an error in cors, I have 2 applications, the first application will fetch data, and the 2nd application becomes a server where I create an api, in application 1 I try to access data from application 2, but it is blocked by cors, I have done several ways such as middleware, here is the code:

import { NextResponse } from "next/server";

export function middleware() {
  // «console. log (req.nexturl);
  const res = NextResponse.next();
  res.headers.append('Access-Control-Allow-Credentials', "true")
  res.headers.append('Access-Control-Allow-Origin', 'http://localhost:3000')
  res.headers.append("Access-Control-Allow-Methods", "GET, DELETE, PATCH, POST, PUT")
  res.headers.append(
    'Access-Control-Allow-Headers',
    'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
  )
  return res
}
export const config = {
  matcher: ['/api/:path*']
}


Please Help
Image
Image

1 Reply

Avatar
Balinese
Hi, usually the CORS settings is set in the backend app (the 2nd app in your case). Have you enabled CORS in there?