javascript 40 lines · 8 steps

Configuring a CORS allowlist in Express

Build an Express app that only accepts cross-origin requests from a curated set of trusted origins.

Explained by highlit
1const express = require('express');
2const cors = require('cors');
3 
4const app = express();
5 
6const allowedOrigins = new Set([
7 'https://app.example.com',
8 'https://admin.example.com',
9]);
10 
11if (process.env.NODE_ENV !== 'production') {
12 allowedOrigins.add('http://localhost:3000');
13}
14 
15const corsOptions = {
16 origin(origin, callback) {
17 if (!origin || allowedOrigins.has(origin)) {
18 return callback(null, true);
19 }
20 return callback(new Error(`Origin ${origin} not allowed by CORS`));
21 },
22 credentials: true,
23 methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
24 allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
25 exposedHeaders: ['X-Request-Id', 'X-RateLimit-Remaining'],
26 maxAge: 86400,
27 optionsSuccessStatus: 204,
28};
29 
30app.use(cors(corsOptions));
31app.options('*', cors(corsOptions));
32 
33app.use((err, req, res, next) => {
34 if (err && err.message && err.message.includes('not allowed by CORS')) {
35 return res.status(403).json({ error: 'CORS_FORBIDDEN', message: err.message });
36 }
37 return next(err);
38});
39 
40module.exports = app;
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A dynamic origin function lets you validate requests against an allowlist instead of hardcoding a single origin.
  2. 2CORS options control not just origins but credentials, methods, headers, and preflight caching behavior.
  3. 3Rejected origins surface as errors, so a dedicated handler is needed to turn them into clean HTTP responses.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Configuring a CORS allowlist in Express — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code