From fc083f8d2a9bcdb77c05ec4f6db4f5019deae521 Mon Sep 17 00:00:00 2001 From: JAYBORICHA Date: Wed, 29 Nov 2023 23:03:14 +0530 Subject: [PATCH] added cors in microservice code --- .env.example | 8 +++++++- package.json | 1 + src/index.ts | 11 +++++++++++ yarn.lock | 12 ++++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 0f0a6df..e3d75dd 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,10 @@ LOG_LEVEL=warn DATABASE_URL=postgres://postgres:12345@localhost:5432/wa_service RECONNECT_INTERVAL=5000 MAX_RECONNECT_RETRIES=5 -AUTH_TOKEN=hello1234 \ No newline at end of file +AUTH_TOKEN=hello1234 +CORSOPTIONS = { + origin: '*', + methods: [ 'GET', 'POST', 'PUT', 'DELETE' ], + allowedHeaders: [ 'Content-Type', 'authorization' ], + credentials: true +} \ No newline at end of file diff --git a/package.json b/package.json index fb1cc59..31488c3 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@prisma/client": "^5.2.0", "@whiskeysockets/baileys": "^6.4.1", "baileys-bottle": "^2.1.2", + "cors": "^2.8.5", "express": "^4.18.2", "express-validator": "^7.0.1", "pm2": "^5.3.0", diff --git a/src/index.ts b/src/index.ts index 2062904..5abe276 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,10 +5,21 @@ import { logger, prisma } from './shared'; import messageRoutes from './router/message'; import { init } from './wa'; import { authMiddleware } from './middleware/auth-middleware'; +import cors from 'cors'; + const app = express(); +var corsOptions : any = process.env.CORSOPTIONS || +{ + origin: '*', + methods: [ 'GET', 'POST', 'PUT', 'DELETE' ], + allowedHeaders: [ 'Content-Type', 'authorization' ], + credentials: true + }; + +app.use(cors(corsOptions)); app.use(bodyParser.json()); app.use(authMiddleware) app.use('/session',sessionRouter); diff --git a/yarn.lock b/yarn.lock index 9ae1f17..c722327 100644 --- a/yarn.lock +++ b/yarn.lock @@ -919,6 +919,14 @@ cookie@0.5.0: resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b" integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw== +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + croner@~4.1.92: version "4.1.97" resolved "https://registry.yarnpkg.com/croner/-/croner-4.1.97.tgz#6e373dc7bb3026fab2deb0d82685feef20796766" @@ -1774,7 +1782,7 @@ nssocket@0.6.0: eventemitter2 "~0.4.14" lazy "~1.0.11" -object-assign@^4.0.1: +object-assign@^4, object-assign@^4.0.1: version "4.1.1" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== @@ -2625,7 +2633,7 @@ validator@^13.9.0: resolved "https://registry.yarnpkg.com/validator/-/validator-13.11.0.tgz#23ab3fd59290c61248364eabf4067f04955fbb1b" integrity sha512-Ii+sehpSfZy+At5nPdnyMhx78fEoPDkR2XW/zimHEL3MyGJQOCQ7WeP20jPYRz7ZCpcKLB21NxuXHF3bxjStBQ== -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==