Skip to content

CORS

Pass a cors object to createServer:

import { createServer } from 'lacis'
createServer('./routes', {
cors: {
origin: 'https://myapp.com',
credentials: true,
},
})

Or use createCorsMiddleware directly to register it as middleware:

import { createCorsMiddleware } from 'lacis'
export const beforeRequest = createCorsMiddleware({
origin: ['https://myapp.com', 'https://admin.myapp.com'],
credentials: true,
})
OptionTypeDefaultDescription
originstring | string[] | RegExp | ((origin: string) => boolean) | '*''*'Allowed origins
methodsstring[]['GET','POST','PUT','DELETE','PATCH','OPTIONS']Allowed HTTP methods
allowedHeadersstring[]['Content-Type','Authorization']Allowed request headers
exposedHeadersstring[]Headers exposed to the browser
credentialsbooleanAllow cookies / credentials
maxAgenumberPreflight cache duration in seconds
// Single origin
origin: 'https://myapp.com'
// Multiple origins
origin: ['https://myapp.com', 'https://admin.myapp.com']
// RegExp
origin: /\.myapp\.com$/
// Custom function
origin: (origin) => origin.endsWith('.myapp.com')
// Allow all (default)
origin: '*'

Lacis handles OPTIONS preflight requests automatically. When a preflight comes in, it responds with 204 and the appropriate Access-Control-Allow-* headers, then stops further processing.