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.
Install
Section titled “Install”GamanJS is a framework for backend applications, you can install using your favorite package manager:
npm install @gaman/corspnpm install @gaman/corsyarn install @gaman/corsbun install @gaman/corsHow to Use
Section titled “How to Use”here is how to implement @gaman/cors
import { cors } from "@gaman/cors"
defineBootstrap((app) => { app.mount( cors({ origin: '*' }) ) ...})if you want specific to a certain route just implement like this
import { cors } from "@gaman/cors"
route.get('/user').middleware(cors())Config
Section titled “Config”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.