honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
418 stars 144 forks source link

[Trpc Server + Cors] The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed #491

Open TheUncharted opened 5 months ago

TheUncharted commented 5 months ago

Hi

I tested hono with trpc inside a lambda and everything is working well except when I try to add the cors middleware. I get this error message

Access to fetch at '' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, *', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

In the browser I have this

image

Here's the code

import { Hono } from "hono";
import { handle } from "hono/aws-lambda";
import { cors } from "hono/cors";

import { trpcServer } from "@hono/trpc-server";

import { appRouter } from "./trpc/trpc";

const app = new Hono();

app.use(
  cors({

    // Only frontend is allowed
    origin: ["http://localhost:3000"],
  })
);

app.use(
  "/trpc/*",
  trpcServer({
    router: appRouter,
  })
);

export const handler = handle(app);
swapnawebdev commented 5 days ago

have you solved this issue , getting the same issue