Security

CORS

Cross-Origin Resource Sharing middleware.

CORS

The @bklarjs/cors package enables Cross-Origin Resource Sharing, allowing your API to be accessed from browsers running on different domains.

Installation

bun add @bklarjs/cors
npm install @bklarjs/cors

Usage

Use it as global middleware to allow all origins (useful for development) or restrict it for production.

import { Bklar } from "bklar";
import { cors } from "@bklarjs/cors";

const app = Bklar();

app.use(
  cors({
    origin: "https://my-frontend.com", // Allow specific origin
    methods: ["GET", "POST", "PUT", "DELETE"], // Allow methods
    credentials: true, // Allow cookies
  })
);

app.listen(3000);

Options

OptionTypeDefaultDescription
originstring | string[] | RegExp"*"Allowed Origin(s).
methodsstring[]['GET', 'POST'...]Allowed HTTP methods.
allowedHeadersstring[]*Allowed request headers.
exposedHeadersstring[][]Headers accessible to the client.
credentialsbooleanfalseAllow cookies/auth headers.
maxAgenumberundefinedPreflight cache duration (seconds).

On this page