File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22# # prisma
33# ##
44DATABASE_URL = mongodb://root:example@localhost:27017/dbname
5- APP_URL = http://localhost
6- PORT = 3000
5+ PORT = 4000
76
87# ##
98# GCP // Firebase credentials.
Original file line number Diff line number Diff line change @@ -5,11 +5,10 @@ services:
55 environment :
66 - NODE_ENV=${NODE_ENV:-development}
77 - DATABASE_URL=${DATABASE_URL}
8- - APP_URL=${APP_URL}
98 - PORT=${PORT}
109 ports :
1110 - " ${PORT}:${PORT}"
1211 volumes :
1312 # set the GOOGLE_APPLICATION_CREDENTIALS env variable to the path of the gcloud.json file
14- # - $HOME/gcloud.json:/app/gcloud.json
13+ # will use the default path if not set. If shell env is set it will take precedence.
1514 - ${GOOGLE_APPLICATION_CREDENTIALS:-$HOME/gcloud.json}:/app/gcloud.json
Original file line number Diff line number Diff line change @@ -10,6 +10,5 @@ if (ENV !== "production") {
1010
1111export const config = {
1212 env : ENV ,
13- port : process . env . PORT || 3000 ,
14- appUrl : process . env . APP_URL ?? "http://localhost:3000"
13+ port : process . env . PORT || 4000
1514} ;
Original file line number Diff line number Diff line change 1+ export const whitelist : RegExp [ ] = [
2+ / ^ h t t p s ? : \/ \/ l o c a l h o s t : 3 0 0 0 $ / ,
3+ / ^ h t t p s ? : \/ \/ e x a m p l e \. c o m $ / ,
4+ / ^ h t t p s ? : \/ \/ s u b d o m a i n \. e x a m p l e \. c o m $ /
5+ // Add more patterns as needed
6+ ] ;
7+
8+ export const corsOptions = {
9+ origin : function ( origin : string | undefined , callback : ( a : null | Error , b ?: boolean ) => void ) {
10+ const isOriginAllowed = origin ? whitelist . some ( ( pattern ) => pattern . test ( origin ) ) : true ;
11+
12+ if ( isOriginAllowed ) {
13+ callback ( null , true ) ;
14+ } else {
15+ callback ( new Error ( "Not allowed by CORS" ) ) ;
16+ }
17+ }
18+ } ;
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ import { pinoHttp } from "pino-http";
33import { config } from "./config.js" ;
44import { routes } from "./routes/index.js" ;
55import cors from "cors" ;
6+ import helmet from "helmet" ;
7+ import cookieParser from "cookie-parser" ;
8+ import { corsOptions } from "./helpers/cors.ts" ;
69
710const { logger } = pinoHttp ( ) ;
811
@@ -20,29 +23,40 @@ checkConfigIsValid();
2023
2124const app = express ( ) ;
2225
26+ //
27+ // Middleware
28+ //
29+
30+ // Logger
2331app . use (
2432 pinoHttp ( {
2533 logger
2634 } )
2735) ;
2836
37+ app . use (
38+ helmet ( {
39+ contentSecurityPolicy : false ,
40+ xDownloadOptions : false
41+ } )
42+ ) ;
43+ app . use ( cookieParser ( ) ) ;
44+
2945// parse application/x-www-form-urlencoded
3046app . use ( express . urlencoded ( { extended : true } ) ) ;
3147
3248// parse application/json
3349app . use ( express . json ( ) ) ;
3450
35- const corsWhitelist = [ `http://localhost:${ config . port } ` , config . appUrl ] ;
36-
37- const corsOptions = {
38- origin : corsWhitelist ,
39- optionsSuccessStatus : 204
40- } ;
41-
51+ // CORS
4252app . use ( cors ( corsOptions ) ) ;
53+ app . options ( "*" , cors ( corsOptions ) ) ;
4354
55+ //
56+ // Routes
57+ //
4458routes ( app ) ;
4559
4660app . listen ( config . port , ( ) => {
47- console . log ( `[server]: Server is running at ${ config . appUrl } : ${ config . port } ` ) ;
61+ logger . info ( `[server]: Server is running on port: ${ config . port } ` ) ;
4862} ) ;
You can’t perform that action at this time.
0 commit comments