Skip to content
LykosJSLYKOSJS
CreatorDeveloperBenchmarkshomeGet started
v1.0.0
OverviewCORSLoggerAuthenticationMiddleware Pipeline
PREVIOUSOverviewNEXT Logger
© 2026 LykosJS · MIT LicenseBun · TypeScript

© 2026 LykosJS. Released under the MIT License.
Built by Zen Allaham.

DocsGitHubX
DOCS/V1.0.0/SECURITY
SECURITY

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.

ts
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 to false.
  • 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-Type andAuthorization.
  • 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 to 86400 seconds.

Allow a single origin

Use a string when your API should accept requests from one specific frontend origin.

ts
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.

ts
1app.use(2  corsMiddleware({3    origin: [4      'https://app.example.com',5      'https://admin.example.com',6    ],7  }),8)
Exact origin matching
The incoming origin must exactly match a configured origin. An origin includes its scheme, hostname and, when present, port. For example, http://localhost:3000 andhttps://localhost:3000 are different origins.

Allow all origins

Use '*' when the API is intended to accept cross-origin requests from any origin and does not require credentialed browser requests.

ts
1app.use(2  corsMiddleware({3    origin: '*',4  }),5)
Wildcard and credentials cannot be combined
LykosJS throws a TypeError iforigin: '*' is combined withcredentials: true. Use an explicit origin or an allowlist when credentialed browser requests are required.

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.

ts
1app.use(2  corsMiddleware({3    origin: 'https://app.example.com',4    credentials: true,5  }),6)
Use trusted origins
Avoid broad origin access when credentials are enabled. Configure only the frontend origins you trust.

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.

ts
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.

ts
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.

ts
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:

ts
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.
  • methods must be a non-empty array.
  • allowedHeaders and exposedHeadersmust be arrays.
  • maxAge must 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.

ts
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})
CORS is not authentication
CORS is a browser security mechanism, not an authentication or authorization system. It does not prevent direct HTTP clients, scripts or servers from sending requests to your API. Always implement proper authentication and authorization independently.