n8n/packages/cli/src/middlewares/cors.ts
कारतोफ्फेलस्क्रिप्ट™ a115baa1ac
fix(core): CORS middleware should not process the entire handler chain on OPTIONS requests (no-changelog) (#5368)
fix(core): CORS middleware should not process the entire handler chain on OPTIONS requests
2023-02-06 12:43:50 +01:00

21 lines
621 B
TypeScript

import type { RequestHandler } from 'express';
export const corsMiddleware: RequestHandler = (req, res, next) => {
if ('origin' in req.headers) {
// Allow access also from frontend when developing
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, sessionid',
);
}
if (req.method === 'OPTIONS') {
res.writeHead(204).end();
} else {
next();
}
};