Skip to content

CORS

@gaman/cors is the official middleware for handling CORS (Cross-Origin Resource Sharing) in the GamanJS framework.

CORS is needed when web frontend applications (for example React, Vue, Svelte, etc.) make requests to server APIs with different domains/ports.

Without CORS, the browser will block cross-origin requests for security reasons.

GamanJS is a framework for backend applications, you can install using your favorite package manager:

Terminal window
npm install @gaman/cors

here is how to implement @gaman/cors

index.ts
import { cors } from "@gaman/cors"
defineBootstrap((app) => {
app.mount(
cors({ origin: '*' })
)
...
})

if you want specific to a certain route just implement like this

AppRoutes.ts
import { cors } from "@gaman/cors"
route.get('/user').middleware(cors())

Here are many configurations for @gaman/cors, here is the explanation

| Name | Type | Default | Description |

|------------------|----------------------|-------------------------------------------------------|-----------|

| origin | string \| string[] | "*" | Allowed origin domain. Can be a single string or array. |

| allowMethods | string[] | ["GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"] | Allowed HTTP methods. |

| allowHeaders | string[] \| null | null | Allowed headers. null means all headers are allowed. |

| maxAge | number | undefined | Preflight cache time in seconds. |

| credentials | boolean | false | Whether to allow credentials (cookie, HTTP authentication, etc.). |

| exposeHeaders | string[] | [] | Headers that can be exposed to the client. |

And there are 3 more default middleware configs please check the following page Middleware Config.