Skip to main content

Enable CORS with cors

Add Cross-Origin Resource Sharing (CORS) to your Cog app using the cors middleware.

Cog gives you full control over the request/response cycle, so CORS is not included by default. You can use the cors package to add CORS headers and allow cross-origin requests easily.

Setup

  1. Install the required package:
npm install cors
  1. If you're using TypeScript, install the type definitions for cors:
npm install --save-dev @types/cors

Example

import { Cog } from "cog-http";
import cors, { type CorsOptions } from "cors";

const app = new Cog();

const corsOptions: CorsOptions = {
origin: "http://localhost:5173"
};

app.use("*", (req, res, next) => {
// Cors expects native req/res objects
return cors(corsOptions)(req.raw, res.raw, next);
});

app.get("/", (_, res) => {
res.send("Hello with CORS!");
});

app.listen(3000, "127.0.0.1", () => {
console.log("Server running at http://127.0.0.1:3000");
});

That’s it! Your Cog app now supports CORS requests.