Aller au contenu

CORS

Passez un objet cors à createServer :

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

Ou utilisez createCorsMiddleware directement pour l’enregistrer comme middleware :

import { createCorsMiddleware } from 'lacis'
export const beforeRequest = createCorsMiddleware({
origin: ['https://monapp.com', 'https://admin.monapp.com'],
credentials: true,
})
OptionTypeDéfautDescription
originstring | string[] | RegExp | ((origin: string) => boolean) | '*''*'Origines autorisées
methodsstring[]['GET','POST','PUT','DELETE','PATCH','OPTIONS']Méthodes HTTP autorisées
allowedHeadersstring[]['Content-Type','Authorization']Headers de requête autorisés
exposedHeadersstring[]Headers exposés au navigateur
credentialsbooleanAutoriser les cookies / credentials
maxAgenumberDurée de cache du preflight en secondes
// Origine unique
origin: 'https://monapp.com'
// Origines multiples
origin: ['https://monapp.com', 'https://admin.monapp.com']
// RegExp
origin: /\.monapp\.com$/
// Fonction personnalisée
origin: (origin) => origin.endsWith('.monapp.com')
// Tout autoriser (défaut)
origin: '*'

Lacis gère les requêtes OPTIONS preflight automatiquement. Quand une requête preflight arrive, il répond avec 204 et les headers Access-Control-Allow-* appropriés, puis arrête tout traitement ultérieur.