Skip to content

Commit 0b6f52d

Browse files
committed
fix local cors policy
1 parent 8480abf commit 0b6f52d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/app.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ const app = express();
2929
export const API_VERSION = 'v1';
3030
export const API_URL = `/api/${API_VERSION}`;
3131

32-
3332
const HOSTNAME = new URL(process.env.FRONTEND_URL || 'http://localhost:3000').hostname;
3433
const domainParts = HOSTNAME.split('.');
3534
const domain = domainParts.slice(domainParts.length - 2).join('.'); /** foo.bar.ch --> domain is bar.ch */
36-
const CORS_APP = new RegExp(`https://(.*\.)?${domain.split('.')[0]}\.${domain.split('.')[1] || ':3000'}$`, 'i')
37-
const CORS_NETLIFY = process.env.NETLIFY_PROJECT_NAME ? new RegExp(`https://deploy-preview-\\d+--${process.env.NETLIFY_PROJECT_NAME}\\.netlify\\.app$`, 'i') : undefined;
38-
export const CORS_ORIGIN = [HOSTNAME, CORS_APP, CORS_NETLIFY].filter(rule => !!rule) as (string | RegExp)[];
35+
const CORS_APP = domain.split('.')[1]
36+
? new RegExp(`https://(.*\.)?${domain.split('.')[0]}\\.${domain.split('.')[1]}$`, 'i')
37+
: 'http://localhost:3000';
38+
const CORS_NETLIFY = process.env.NETLIFY_PROJECT_NAME
39+
? new RegExp(`https://deploy-preview-\\d+--${process.env.NETLIFY_PROJECT_NAME}\\.netlify\\.app$`, 'i')
40+
: undefined;
41+
export const CORS_ORIGIN = [HOSTNAME, CORS_APP, CORS_NETLIFY].filter((rule) => !!rule) as (string | RegExp)[];
3942

4043
/**
4144
* this is not needed when running behind a reverse proxy
@@ -48,8 +51,7 @@ app.use(
4851
cors({
4952
credentials: true,
5053
origin: CORS_ORIGIN,
51-
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD'],
52-
54+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD']
5355
})
5456
);
5557

0 commit comments

Comments
 (0)