Skip to content

Configuration ​

cors(options?) takes one object; every option is optional. The options are the Options type; resolveOptions() turns them into ResolvedOptions with the defaults below.

Options ​

OptionTypeDefaultDescription
originboolean | string | RegExp | (string | RegExp)[] | (origin: string) => boolean'*'Which origins get Access-Control-Allow-Origin, and with which value; false turns the plugin off
methods'*' | string[]'*'Access-Control-Allow-Methods on a preflight, joined with ,
allowHeaders'*' | string[]'*'Access-Control-Allow-Headers on a preflight; '*' mirrors the request's Access-Control-Request-Headers
exposeHeaders'*' | string[]'*'Access-Control-Expose-Headers on the other responses, joined with ,
credentialsbooleanfalseSends Access-Control-Allow-Credentials: true on every response
maxAgestring | number | falsefalseAccess-Control-Max-Age on a preflight, in seconds; false sends none
preflight{ status?: number; continue?: boolean }{ status: 204, continue: false }Status of the preflight response, or let your own OPTIONS handler answer

For methods, allowHeaders and exposeHeaders, ['*'] is the same as '*'. An empty array sends no Access-Control-Allow-Methods or Access-Control-Expose-Headers; for allowHeaders it is the same as '*'.

origin ​

ValueAccess-Control-Allow-OriginVary: origin
'*' (default)*, on every responseno
trueThe request's Origin; none when the request has no Originyes
Any other string, such as 'https://app.example.com' or 'null'That string, on every response, whatever the request's Originyes
RegExpThe request's Origin when the pattern matches ityes
Array of strings and RegExpsThe request's Origin when an entry matches it: a string by equality, a RegExp with test()yes
Function (origin) => booleanThe request's Origin when the function returns true; not called without an Originyes
falseNone: the plugin passes every request on, preflights too, and sets no headerno

A fixed string is not a check: other origins get the same value and the browser rejects the response. To send no header to other origins, use an array of one (Allow One Origin).

When no entry matches, the response has no Access-Control-Allow-Origin, so the browser blocks it; the plugin still sets the other headers and a preflight is still answered. Vary: origin is sent for every option except '*' and false, matching or not, so a shared cache does not serve the answer to one origin to another. A RegExp with the g or y flag is copied without those flags before each test, so its lastIndex does not make the result alternate between requests.

ts
import { App, defineCoreHandler } from 'routup';
import { cors } from '@routup/cors';

export const app = new App();

app.use(cors({
    origin: [
        'https://app.example.com',
        /\.staging\.example\.com$/,
    ],
}));

app.get('/', defineCoreHandler(() => 'Hello, World!'));

An Allow-List shows a function.

methods ​

The methods a preflight allows, as a list or '*'. A literal * is only a wildcard for requests without credentials, so with credentials a '*' is sent as GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS.

allowHeaders ​

The request headers a preflight allows. With '*', an empty array or ['*'], the preflight answers with the request's own Access-Control-Request-Headers, so a literal * is never sent and this is safe with credentials; a preflight without that header gets no Access-Control-Allow-Headers. Whenever the header is sent, Vary: access-control-request-headers is appended.

exposeHeaders ​

The response headers a script may read beyond the CORS-safelisted ones. A browser treats * as a wildcard only for requests without credentials; with credentials, list the headers you need, such as ['x-request-id'].

credentials ​

true sends Access-Control-Allow-Credentials: true, on a preflight and on the other responses, so the browser sends cookies and reads the response. Browsers do not accept a literal * on a request with credentials, so the plugin handles each wildcard:

Option with '*'With credentials: true
methodsExpanded to GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS
allowHeadersMirrors Access-Control-Request-Headers, as without credentials
exposeHeadersSent as *; the response is not blocked, but scripts read only the CORS-safelisted headers. The plugin logs a warning
originSent as *, so the browser blocks the response. The plugin logs a warning and does not switch to reflecting the origin, which would let any site send requests with the user's cookies. Use true or an allow-list

The warnings are written with console.warn when the plugin is installed with app.use(), or when createHandler() is called; not per request. Credentials is a complete setup.

maxAge ​

How long, in seconds, the browser may reuse a preflight answer. A number is sent as its string; false (the default) sends no Access-Control-Max-Age, and the browser applies its own default. See Preflight Caching.

preflight ​

KeyTypeDefaultDescription
statusnumber204Status of the preflight response, for clients that do not accept 204
continuebooleanfalseSet the preflight headers, then call event.next() instead of answering

With continue, your OPTIONS route answers the preflight and the headers are already on event.response (Your Own Preflight Handler). Without such a route, routup answers the OPTIONS request itself, with status 200 and an Allow header.