2022-12-07 06:13:36 -08:00
|
|
|
import type { RequestHandler } from 'express';
|
|
|
|
|
|
|
|
export const corsMiddleware: RequestHandler = (req, res, next) => {
|
2023-01-04 02:38:48 -08:00
|
|
|
if ('origin' in req.headers) {
|
2022-12-07 06:13:36 -08:00
|
|
|
// 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',
|
|
|
|
);
|
|
|
|
}
|
2023-02-06 03:43:50 -08:00
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
res.writeHead(204).end();
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
2022-12-07 06:13:36 -08:00
|
|
|
};
|