CORS configuration
Control cross-origin requests with configurable origins, credentials, HTTP methods, headers and preflight caching.
Configure CORS
CORS (Cross-Origin Resource Sharing) controls which browser origins are allowed to access your API. LykosJS providescorsMiddleware to configure CORS policies globally through the middleware system.
1import { corsMiddleware } from 'lykosjs'2 3app.use(4 corsMiddleware({5 origin: ['https://app.example.com'],6 credentials: true,7 methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],8 allowedHeaders: ['Content-Type', 'Authorization'],9 exposedHeaders: ['X-Request-Id'],10 maxAge: 86400,11 }),12)Configuration options
The corsMiddleware function accepts aCorsOptions object. The origin property is required, while the remaining properties are optional.
- origin — Required. Accepts a single origin, an array of allowed origins, or
'*'to allow any origin. - credentials — Allows credentialed browser requests by setting
Access-Control-Allow-Credentials. Defaults tofalse. - methods — Specifies the HTTP methods allowed for cross-origin requests. Defaults to GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS.
- allowedHeaders — Specifies the request headers browsers may use. Defaults to
Content-TypeandAuthorization. - exposedHeaders — Specifies response headers that browser JavaScript is allowed to access. Defaults to an empty array.
- maxAge — Sets the preflight cache duration in seconds through
Access-Control-Max-Age. Defaults to86400seconds.
Allow a single origin
Use a string when your API should accept requests from one specific frontend origin.
1app.use(2 corsMiddleware({3 origin: 'https://app.example.com',4 }),5)Allow multiple origins
Provide an array when multiple frontend applications need access. The middleware checks the incoming Origin header against the configured values.
1app.use(2 corsMiddleware({3 origin: [4 'https://app.example.com',5 'https://admin.example.com',6 ],7 }),8)Allow all origins
Use '*' when the API is intended to accept cross-origin requests from any origin and does not require credentialed browser requests.
1app.use(2 corsMiddleware({3 origin: '*',4 }),5)Enable credentials
Set credentials: true to includeAccess-Control-Allow-Credentials: true in responses for allowed origins. This is commonly needed when browser requests use cookies or other credentials.
1app.use(2 corsMiddleware({3 origin: 'https://app.example.com',4 credentials: true,5 }),6)Configure methods and headers
Use methods to define the methods advertised byAccess-Control-Allow-Methods, andallowedHeaders to define the request headers advertised by Access-Control-Allow-Headers.
1app.use(2 corsMiddleware({3 origin: 'https://app.example.com',4 methods: ['GET', 'POST', 'PATCH', 'DELETE'],5 allowedHeaders: [6 'Content-Type',7 'Authorization',8 'X-Request-Id',9 ],10 }),11)These options configure the CORS response headers. The middleware does not independently validate each incoming method or request header against these lists.
Expose response headers
Browsers restrict which response headers frontend JavaScript can read. Use exposedHeaders to make additional response headers accessible to browser code.
1app.use(2 corsMiddleware({3 origin: 'https://app.example.com',4 exposedHeaders: [5 'X-Request-Id',6 'X-RateLimit-Remaining',7 ],8 }),9)Preflight requests
Browsers may send an OPTIONS request before the actual cross-origin request. This is known as a CORS preflight request. LykosJS handles requests with theOPTIONS method directly in the middleware.
1app.use(2 corsMiddleware({3 origin: 'https://app.example.com',4 methods: ['GET', 'POST', 'PUT', 'DELETE'],5 allowedHeaders: ['Content-Type', 'Authorization'],6 maxAge: 3600,7 }),8)The middleware returns a 204 No Content response for every OPTIONS request it receives, without calling the next middleware or continuing to route handling. For allowed origins, it includes the configured CORS headers.
The maxAge option controls how long browsers may cache the preflight result, in seconds. For example,3600 represents one hour.
Default configuration
When optional properties are omitted, LykosJS uses the following defaults:
1{2 credentials: false,3 methods: [4 'GET',5 'HEAD',6 'POST',7 'PUT',8 'PATCH',9 'DELETE',10 'OPTIONS',11 ],12 allowedHeaders: ['Content-Type', 'Authorization'],13 exposedHeaders: [],14 maxAge: 86400,15}The origin option has no default and must always be provided.
Option validation
LykosJS validates several configuration constraints whencorsMiddleware is created:
origin: '*'cannot be combined withcredentials: true.methodsmust be a non-empty array.allowedHeadersandexposedHeadersmust be arrays.maxAgemust be a non-negative integer.
Common mistakes
Avoid these configurations because they violate the middleware's supported options or may not produce the intended CORS policy.
1// Incorrect: wildcard origin with credentials2corsMiddleware({3 origin: '*',4 credentials: true,5})6 7// Incorrect: empty methods array8corsMiddleware({9 origin: 'https://app.example.com',10 methods: [],11})12 13// Incorrect: maxAge must be a non-negative integer14corsMiddleware({15 origin: 'https://app.example.com',16 maxAge: -1,17})18 19// Correct: explicit trusted origin with credentials20corsMiddleware({21 origin: 'https://app.example.com',22 credentials: true,23})