Documentation
Docs
Introduction
Getting Started
Tutorial: build an app
Core Concepts
Routing
Server-Side Rendering
Mimir, state management
Pages & Layouts
API Routes
Styling & Theming
Building & Deploying
API Reference
Guides
Recipes
FAQ (use cases)
cors()
Let browsers on other origins call your API routes, and handle the preflight request they send first.
Use case
The browser blocks cross-origin requests unless the server opts in. If your front-end runs on a different
origin than your rune app (a separate SPA, a mobile web client, a partner site), it cannot read your API
responses until you send the Access-Control-* headers. cors sends them, and answers the preflight
OPTIONS request the browser fires before the real one.
How it works
For a preflight OPTIONS request it short-circuits with 204 No Content plus the allow headers. For a real
request it sets the headers and calls next(). With an origin allow-list, only listed origins are echoed
back in Access-Control-Allow-Origin; everything else simply receives no CORS header (and the browser
blocks it).
Configuration
| Option | Default | Notes |
|---|---|---|
origin | "*" | A string, an array of allowed origins, or "*". With an array, only listed origins are allowed and echoed back. |
methods | GET, POST, PUT, DELETE, OPTIONS | Allowed methods. |
headers | Content-Type, Authorization | Allowed request headers. |
credentials | false | Set Access-Control-Allow-Credentials: true (required for cookies). |
maxAge | 86400 | Preflight cache seconds. |
Example
Notes
Do not combine
origin: "*"withcredentials: true, browsers reject it. Use an explicit allow-list when you need cookies. And if your front-end is the same rune app (the common case), you do not need CORS at all, same-origin requests are never blocked.
Next: rateLimit.