2021-12-29 23:08:41 +00:00
|
|
|
import { Application } from 'express';
|
|
|
|
import { Logger, LoggerService } from './logger.js';
|
2022-01-16 20:55:50 +00:00
|
|
|
import { Common, CommonService } from './common.js';
|
2021-12-29 23:08:41 +00:00
|
|
|
|
|
|
|
class CORS {
|
|
|
|
|
|
|
|
public logger: LoggerService = Logger;
|
2022-01-16 20:55:50 +00:00
|
|
|
public common: CommonService = Common;
|
2021-12-29 23:08:41 +00:00
|
|
|
|
|
|
|
public mount(app: Application): Application {
|
2022-01-16 20:55:50 +00:00
|
|
|
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') {
|
2022-03-05 17:13:02 +00:00
|
|
|
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();
|
|
|
|
});
|
2022-01-16 20:55:50 +00:00
|
|
|
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;
|