CORS
Configuration
Section intitulée « Configuration »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,})| Option | Type | Défaut | Description |
|---|---|---|---|
origin | string | string[] | RegExp | ((origin: string) => boolean) | '*' | '*' | Origines autorisées |
methods | string[] | ['GET','POST','PUT','DELETE','PATCH','OPTIONS'] | Méthodes HTTP autorisées |
allowedHeaders | string[] | ['Content-Type','Authorization'] | Headers de requête autorisés |
exposedHeaders | string[] | — | Headers exposés au navigateur |
credentials | boolean | — | Autoriser les cookies / credentials |
maxAge | number | — | Durée de cache du preflight en secondes |
Formats d’origine
Section intitulée « Formats d’origine »// Origine uniqueorigin: 'https://monapp.com'
// Origines multiplesorigin: ['https://monapp.com', 'https://admin.monapp.com']
// RegExporigin: /\.monapp\.com$/
// Fonction personnaliséeorigin: (origin) => origin.endsWith('.monapp.com')
// Tout autoriser (défaut)origin: '*'Preflight
Section intitulée « Preflight »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.