Skip to main content

corsHandler

function corsHandler(allowedOrigins: string = "*", config: Object = {})

Middleware to handle Cross-Origin Resource Sharing (CORS).

Returns: function

  • Express middleware function to handle CORS requests.
PARAMETERTYPEOPTIONALDEFAULTDESCRIPTION
allowedOriginsstring✔️"*"Comma-separated list of allowed origins or "*" for all origins.
configObject✔️Additional configuration options for CORS (excluding origin).
See the official CORS package documentation for more details:

See:

const express = require("express");

const app = express();
app.use(corsHandler("https://example.com,https://another.com", { optionsSuccessStatus: 200 }));

app.get("/", (req, res) => {
res.json({ message: "CORS enabled" });
});