CORS
Learn how to configure Cross-Origin Resource Sharing (CORS) for your API routes.
What is CORS?
Cross-Origin Resource Sharing (CORS) is a security feature implemented by browsers that restricts web pages from making requests to a different domain than the one that served the web page. CORS headers allow servers to specify which origins are permitted to access their resources.
Bini.js includes built-in CORS support for API routes, making it easy to build APIs that can be accessed from different origins.
Default Configuration
CORS is enabled by default for all API routes in dev and preview. The default configuration includes:
- Access-Control-Allow-Origin:
*(all origins) - Access-Control-Allow-Methods:
GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD - Access-Control-Allow-Headers:
Content-Type, Authorization, X-Request-ID - Access-Control-Max-Age:
86400(24 hours for preflight requests)
Disabling CORS
Disable CORS by setting cors: false in your biniroute() configuration:
CORS with Hono
When using Hono for your API routes, you can configure CORS per route or globally using Hono's cors middleware:
| Option | Type | Description |
|---|---|---|
| origin | string | string[] | "*" | Allowed origins (default: "*") |
| allowMethods | string[] | Allowed HTTP methods |
| allowHeaders | string[] | Allowed request headers |
| maxAge | number | Preflight cache duration in seconds |
| credentials | boolean | Allow credentials (cookies, auth) |
| exposeHeaders | string[] | Headers exposed to the browser |
Custom CORS Configuration
For more granular control, you can implement custom CORS handling in your API routes:
Production Deployment
When deploying to production, the same CORS configuration applies. For platform-specific configuration:
- bini-server (Node.js): Uses the same CORS configuration from your
vite.config.ts - Netlify Edge Functions: Uses the CORS headers set in your Hono app
- Vercel Edge: Uses the CORS headers set in your Hono app
- Cloudflare Workers: Uses the CORS headers set in your Hono app
* to improve security.