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

10 months ago

(no markdown available for this content)

Solved$10 Bounty

Pinned Solution

clashing
HOBBY

10 months 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

11 Replies

irazvan2745
FREEOP

10 months ago

these images didnt post from discord


clashing
HOBBY

10 months 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
FREEOP

10 months ago


irazvan2745
FREEOP

10 months ago

these are the headers of a options request

1407222266340839484


clashing
HOBBY

10 months 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
FREEOP

10 months ago

I already do that

Attachments


irazvan2745

![](https://station-server.railway.com/attachments/att_01k30ckt8mesj8fgb2ja6mt69r)I already do that

clashing
HOBBY

10 months 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
FREEOP

10 months 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
HOBBY

10 months 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
FREEOP

10 months 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
HOBBY

10 months ago

Great, glad I helped you ✌


Status changed to Solved angelo-railway 10 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...