From 3ba86dd29c2a3daf6dd51335850a20a23bee1386 Mon Sep 17 00:00:00 2001 From: Ian Goldfarb Date: Thu, 8 Nov 2018 10:44:27 -0800 Subject: [PATCH] Adding CORS headers This may not be the best option, but I've used it in the past with Express apps so wanted to send as an optional solution for the CORS issue. Cheers, Ian --- src/server/express.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/server/express.ts b/src/server/express.ts index 175b44c..1526cac 100644 --- a/src/server/express.ts +++ b/src/server/express.ts @@ -27,6 +27,22 @@ export class ExpressServer implements IServer { this.app.use(bodyParser.urlencoded({ extended: false })); this.app.use(bodyParser.json()); this.app.use(compression()); + // Adding CORS headers + this.app.use(function (req, res, next) { + let allowedOrigins = [ + 'https://dashboard.supplyhub.com', + 'https://dev-dashboard.supplyhub.com', + 'http://localhost:4200' + ]; + let origin = req.headers.origin; + if (allowedOrigins.indexOf(origin) > -1) { + res.setHeader('Access-Control-Allow-Origin', origin); + } + res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS'); + res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + res.header('Access-Control-Allow-Credentials', true); + return next(); + }); } public start(): Promise {