2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-10-31 09:20:27 +00:00
RTL/backend/utils/cors.js

26 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-12-29 23:08:41 +00:00
import { Logger } from './logger.js';
import { Common } from './common.js';
2021-12-29 23:08:41 +00:00
class CORS {
constructor() {
this.logger = Logger;
this.common = Common;
2021-12-29 23:08:41 +00:00
}
mount(app) {
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'CORS', msg: 'Setting up CORS..' });
2021-12-29 23:08:41 +00:00
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, filePath');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
if (process.env.NODE_ENV === 'development') {
res.setHeader('Access-Control-Allow-Credentials', 'true');
2021-12-29 23:08:41 +00:00
res.setHeader('Access-Control-Allow-Origin', req.headers.origin ? req.headers.origin : req.headers.host ? req.headers.host : '');
}
next();
});
this.logger.log({ selectedNode: this.common.initSelectedNode, level: 'INFO', fileName: 'CORS', msg: 'CORS Set' });
2021-12-29 23:08:41 +00:00
return app;
}
;
}
export default new CORS;