← All @molecule/* packages · App templates
@molecule/api-middleware-corsMiddleware · core · API (Node) · v1.0.1 · Apache-2.0
CORS middleware interface
npm install @molecule/api-middleware-cors@molecule/api-middleware-cors is core middleware for the API, mounted once at startup.
import { createCorsMiddleware } from '@molecule/api-middleware-cors'
// Explicit allowlist from env; credentials on for cookie/bearer auth.
app.use(
createCorsMiddleware({
origin: [process.env.APP_ORIGIN, process.env.SITE_ORIGIN].filter(Boolean),
credentials: true,
}),
)Providers (1): @molecule/api-middleware-cors-express
Works with: @molecule/api-bond, @molecule/api-secrets
Secrets: APP_ORIGIN (optional), SITE_ORIGIN (optional), APP_URL_SCHEME (optional)
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
CORS middleware for molecule.dev.
Core interface — the implementation is provided via a bond. Install and
wire a CORS bond (e.g. @molecule/api-middleware-cors-express, which
registers corsFactory via setCorsFactory()) or cors /
createCorsMiddleware() throw "No CORS ... bonded" on first use.
APP_ORIGIN / SITE_ORIGIN are the conventional env vars for the
allowlist (registered as optional secrets by this package).
import { createCorsMiddleware } from '@molecule/api-middleware-cors'
// Explicit allowlist from env; credentials on for cookie/bearer auth.
app.use(
createCorsMiddleware({
origin: [process.env.APP_ORIGIN, process.env.SITE_ORIGIN].filter(Boolean),
credentials: true,
}),
)
middleware
npm install @molecule/api-middleware-cors @molecule/api-bond @molecule/api-secrets
CorsOptionsCORS configuration options.
interface CorsOptions {
origin?:
| string
| string[]
| boolean
| RegExp
| (string | RegExp | null | undefined)[]
| ((origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => void)
methods?: string | string[]
allowedHeaders?: string | string[]
exposedHeaders?: string | string[]
credentials?: boolean
maxAge?: number
preflightContinue?: boolean
optionsSuccessStatus?: number
}
CorsFactoryFactory function type for creating CORS middleware with custom options.
type CorsFactory = (options: CorsOptions) => Middleware
MiddlewareGeneric middleware type — framework-agnostic.
type Middleware = (req: unknown, res: unknown, next: (err?: unknown) => void) => void
cors(req, res, next)Default CORS middleware that delegates to the bonded implementation.
function cors(req: unknown, res: unknown, next: (err?: unknown) => void): void
req — The incoming request object.res — The response object.next — The next middleware function.Returns: The result of the bonded CORS handler invocation.
createCorsMiddleware(options)Creates CORS middleware with custom options via the bonded factory.
function createCorsMiddleware(options: CorsOptions): Middleware
options — CORS options (origins, methods, headers, credentials, etc.).Returns: A middleware function that handles CORS headers.
getCors()Gets the bonded CORS middleware.
function getCors(): Middleware
Returns: The bonded CORS middleware function.
getCorsFactory()Gets the bonded CORS factory.
function getCorsFactory(): CorsFactory | null
Returns: The factory function, or null if none has been bonded.
hasCors()Checks if a CORS middleware has been bonded.
function hasCors(): boolean
Returns: true if CORS middleware is available.
setCors(middleware)Bonds a CORS middleware implementation for use by getCors() and cors.
function setCors(middleware: Middleware): void
middleware — The middleware function that handles CORS headers.setCorsFactory(factory)Bonds a CORS factory for creating middleware with custom options (origins, methods, headers).
function setCorsFactory(factory: CorsFactory): void
factory — A function that creates CORS middleware from options.Peer dependencies:
@molecule/api-bond ^1.0.1@molecule/api-secrets ^1.0.1APP_ORIGIN (optional)SITE_ORIGIN (optional)APP_URL_SCHEME (optional)@molecule/api-bond@molecule/api-secretsCORS decides which browser ORIGINS may call your API (with credentials). Configure an
explicit allowlist from env (APP_ORIGIN / SITE_ORIGIN) via {@link CorsOptions}.origin
— don't hand-roll Access-Control-* headers.
origin: '*' with credentials: true is invalid AND insecure. The browser rejects a
wildcard on a credentialed (cookie / Authorization) request, and reflecting arbitrary
origins with credentials is a CSRF / data-exfiltration hole. For an app that sends the
session cookie or a bearer token, set a SPECIFIC origin allowlist + credentials: true.