cors.ts•563 B
import cors from 'cors';
/**
* 配置 CORS 中间件
*/
export function configureCors(): cors.CorsOptions {
const allowedOrigins = (process.env.ALLOWED_ORIGINS || '')
.split(',')
.map(s => s.trim())
.filter(Boolean);
return {
origin: (origin, callback) => {
if (!origin) return callback(null, true);
if (allowedOrigins.length === 0) return callback(null, true);
if (allowedOrigins.includes(origin)) return callback(null, true);
return callback(new Error('Not allowed by CORS'));
},
credentials: true
};
}