CORS issues on a hono app with cors set to "*"

irazvan2745
PRO

14 days ago

Solved$10 Bounty

10 Replies

irazvan2745
PRO

14 days ago

these images didnt post from discord


clashing
HOBBYTop 1% Contributor

14 days ago

Can you provide your railway-service URL for which you are seeing these errors!


clashing

Can you provide your railway-service URL for which you are seeing these errors!

irazvan2745
PRO

14 days ago


irazvan2745
PRO

14 days ago

these are the headers of a options request

1407222266340839400


clashing
HOBBYTop 1% Contributor

14 days ago

From the domains & the request params, I can see that you are using a Custom Domain. The CORS error arises for those deployments that have a Custom Domain linked to them. And for those, you can just do this to get over the issue:

const cors = require("cors");

app.use(cors());

Make sure you install cors before adding these lines to your server file.

If that helps you clear your issue, do mark this as the solution


irazvan2745
PRO

14 days ago

I already do that

Attachments


irazvan2745

I already do that

clashing
HOBBYTop 1% Contributor

14 days ago

No need to use this extensive CORS configuration!

app.use(cors()) works for every service for me! Just give it a try (add this line right after instantiating/creating the app object)


irazvan2745
PRO

14 days ago

with the cors package im getting this type error ```No overload matches this call.

Overload 1 of 21, '(...handlers: MiddlewareHandler<BlankEnv, never, {}>[]): Hono<{}, BlankSchema, "/">', gave the following error.

Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'MiddlewareHandler<BlankEnv, never, {}>'.

Target signature provides too few arguments. Expected 3 or more, but got 2.

Overload 2 of 21, '(handler: MiddlewareHandler<BlankEnv, never, {}>): Hono<{}, BlankSchema, "/">', gave the following error.

Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'MiddlewareHandler<BlankEnv, never, {}>'.

Target signature provides too few arguments. Expected 3 or more, but got 2.

Overload 3 of 21, '(path: string, ...handlers: MiddlewareHandler<BlankEnv, string, {}>[]): Hono<BlankEnv, BlankSchema, "/">', gave the following error.

Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'string'. (ts 2769)```

and with hono/cors its the same error CORS No Allow Credentials


irazvan2745

with the cors package im getting this type error ```No overload matches this call.Overload 1 of 21, '(...handlers: MiddlewareHandler<BlankEnv, never, {}>[]): Hono<{}, BlankSchema, "/">', gave the following error.Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'MiddlewareHandler<BlankEnv, never, {}>'.Target signature provides too few arguments. Expected 3 or more, but got 2.Overload 2 of 21, '(handler: MiddlewareHandler<BlankEnv, never, {}>): Hono<{}, BlankSchema, "/">', gave the following error.Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'MiddlewareHandler<BlankEnv, never, {}>'.Target signature provides too few arguments. Expected 3 or more, but got 2.Overload 3 of 21, '(path: string, ...handlers: MiddlewareHandler<BlankEnv, string, {}>[]): Hono<BlankEnv, BlankSchema, "/">', gave the following error.Argument of type '(req: CorsRequest, res: { statusCode?: number | undefined; setHeader(key: string, value: string): any; end(): any; }, next: (err?: any) => any) => void' is not assignable to parameter of type 'string'. (ts 2769)```and with hono/cors its the same error CORS No Allow Credentials

clashing
HOBBYTop 1% Contributor

14 days ago

Okay, got it! You are not using express server, and that's why seeing the error with the regular CORS package. You used hono/cors package, so can you try this:

app.use(
  cors({
    origin: (origin) => {
      return ["https://dash.waffle.host", "https://api.waffle.host"].includes(origin)
        ? origin
        : "";
    },
    allowHeaders: ["Content-Type", "Authorization"],
    allowMethods: ["GET", "POST", "OPTIONS"],
    credentials: true,
  })
);

I have removed the * from the config, so that it affects all available routes

Note: These CORS errors only originate when a CUSTOM-DOMAIN is linked to the Railway service


irazvan2745
PRO

14 days ago

You are amazing, Thank you so much. Cors always gave me problems


irazvan2745

You are amazing, Thank you so much. Cors always gave me problems

clashing
HOBBYTop 1% Contributor

14 days ago

Great, glad I helped you


Status changed to Solved angelo-railway 14 days ago


CORS issues on a hono app with cors set to "*" - Railway Help Station