CORS implemented in a static manner: unable to configure on another machine
This commit is contained in:
@@ -20,7 +20,21 @@ const server = http.createServer(app);
|
||||
|
||||
// Middleware
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
const allowedOrigins = process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : [];
|
||||
|
||||
const corsOptions = {
|
||||
origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
|
||||
// Allow same-origin requests (origin is undefined) and requests from the whitelisted origins
|
||||
if (!origin || allowedOrigins.includes(origin)) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
console.warn(`CORS: Blocked request from origin: ${origin}`);
|
||||
callback(new Error('Not allowed by CORS'));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
app.use(cors(corsOptions));
|
||||
|
||||
// Public API Routes
|
||||
app.use('/api/auth', authRouter);
|
||||
|
||||
Reference in New Issue
Block a user