From c6a557cac34e8c4ee5a46442f77d1920645a5daf Mon Sep 17 00:00:00 2001 From: viv3kk <30946383+viv3kk@users.noreply.github.com> Date: Tue, 21 Apr 2020 16:52:42 +0530 Subject: [PATCH 1/2] Update index.js --- src/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 753c400..e18a7a7 100755 --- a/src/index.js +++ b/src/index.js @@ -70,7 +70,23 @@ if (program.verbose) { }); } -app.use(cors()); +var corsOptionsDelegate = function (req, callback) { + var corsOptions = { + origin: true, + credentials: true, + methods: "GET,HEAD,PUT,PATCH,POST,DELETE", + preflightContinue: false, + optionsSuccessStatus: 204 + }; + callback(null, corsOptions); +}; + +/*By default "cors()" set the Access-Control-Allow-Origin to "*" which Chrome will reject when the request was made with "credentials" flag enabled. It throws error something like: +A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. */ + +app.use(cors(corsOptionsDelegate)); + +//app.use(cors()); for (var path in config.map) { (function (basePath) { From 157bdd87e06119f88aeb9d87c74f5d3fa96bf700 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Murat=20=C3=87orlu?= <127687+muratcorlu@users.noreply.github.com> Date: Mon, 19 Dec 2022 22:19:43 +0100 Subject: [PATCH 2/2] refactor: cors options parameter simplified --- src/index.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index e18a7a7..23b85c8 100755 --- a/src/index.js +++ b/src/index.js @@ -70,23 +70,15 @@ if (program.verbose) { }); } -var corsOptionsDelegate = function (req, callback) { - var corsOptions = { - origin: true, - credentials: true, - methods: "GET,HEAD,PUT,PATCH,POST,DELETE", - preflightContinue: false, - optionsSuccessStatus: 204 - }; - callback(null, corsOptions); +var corsOptions = { + origin: true, + credentials: true, + methods: "GET,HEAD,PUT,PATCH,POST,DELETE", + preflightContinue: false, + optionsSuccessStatus: 204 }; -/*By default "cors()" set the Access-Control-Allow-Origin to "*" which Chrome will reject when the request was made with "credentials" flag enabled. It throws error something like: -A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. */ - -app.use(cors(corsOptionsDelegate)); - -//app.use(cors()); +app.use(cors(corsOptions)); for (var path in config.map) { (function (basePath) {