mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-04 17:37:28 -08:00
a115baa1ac
fix(core): CORS middleware should not process the entire handler chain on OPTIONS requests
21 lines
621 B
TypeScript
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();
|
|
}
|
|
};
|