How to fix CORS error in Next.js?

export async function POST(req: NextRequest) {
  // req.headers.set("Access-Control-Allow-Origin", "http://localhost:3000/")
  req.headers.set("Access-Control-Allow-Origin", "*")
  req.headers.set("Access-Control-Allow-Methods", "*")

  cors({ origin: "*" })

  const { env, userId, secretKey } = await req.json()

  if (!userId) {
    return NextResponse.json({ error: "userId is missing" }, { status: 400, headers: corsHeaders })
  }
image.png
image.png
Was this page helpful?