Skip to content

Commit f68e7d4

Browse files
committed
feat: Allow cors logic modified
1 parent 40d3b2b commit f68e7d4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

sample.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ DB_USER=c7f
55
DB_PASSWORD=c7f
66
DB_NAME=c7f
77
JWT_SECRET=secret
8+
ALLOW_CORS=true
9+
ALLOWED_ORIGINS=http://localhost:3001,http://locahost:5000

src/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ const app = express();
1313
app.set('PORT', process.env.PORT || 3000);
1414
app.use(bodyParser.json());
1515

16-
const { NODE_ENV } = process.env;
17-
if (NODE_ENV === 'dev') {
18-
app.use(cors());
16+
const { ALLOW_CORS, ALLOWED_ORIGINS } = process.env;
17+
if (ALLOW_CORS === 'true') {
18+
const corsOptions = {
19+
origin: ALLOWED_ORIGINS ? ALLOWED_ORIGINS.split(',') : '*',
20+
};
21+
22+
app.use(cors(
23+
corsOptions,
24+
));
1925
}
2026

2127
app.use(appRoutes);

0 commit comments

Comments
 (0)