Skip to content

Commit d71c0ea

Browse files
committed
Fix cors
1 parent 35ef4bf commit d71c0ea

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

.env.sample

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ CLIENT_URLS=http://localhost:5173,http://localhost:5174
22
MAIL_PASSWORD=my-app-password
33
MAIL_USERNAME=[email protected]
44
MAIL_SERVICE=gmail
5-
PORT=5000
5+
PORT=5000
6+
NODE_END=dev

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
/dist
1314

1415
# misc
1516
.DS_Store

src/app.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ const app = express();
88
app.use(express.json());
99
app.use(
1010
cors({
11-
origin: function (origin, callback) {
12-
if (origin && config.server.allowedOrigins.indexOf(origin) !== -1) {
13-
callback(null, true);
14-
} else {
15-
callback(new Error('Not allowed by CORS'));
16-
}
17-
},
11+
origin: config.dev
12+
? '*'
13+
: (origin, callback) => {
14+
if (origin && config.server.allowedOrigins.indexOf(origin) !== -1) {
15+
callback(null, true);
16+
} else {
17+
callback(null, false);
18+
}
19+
},
1820
}),
1921
);
2022

src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { config as dotenvConfig } from 'dotenv';
33
dotenvConfig();
44

55
export const config = {
6+
dev: process.env.NODE_ENV === 'dev',
67
server: {
78
port: process.env.PORT || 5000,
89
allowedOrigins: process.env.CLIENT_URLS ? process.env.CLIENT_URLS.split(',') : [],

0 commit comments

Comments
 (0)