@@ -29,13 +29,16 @@ const app = express();
2929export const API_VERSION = 'v1' ;
3030export const API_URL = `/api/${ API_VERSION } ` ;
3131
32-
3332const HOSTNAME = new URL ( process . env . FRONTEND_URL || 'http://localhost:3000' ) . hostname ;
3433const domainParts = HOSTNAME . split ( '.' ) ;
3534const 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