Skip to content

Commit 10642e8

Browse files
committed
feat: update cors and base config
1 parent b3c7ba7 commit 10642e8

5 files changed

Lines changed: 43 additions & 14 deletions

File tree

.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
## prisma
33
###
44
DATABASE_URL=mongodb://root:example@localhost:27017/dbname
5-
APP_URL=http://localhost
6-
PORT=3000
5+
PORT=4000
76

87
###
98
# GCP // Firebase credentials.

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff 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

src/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ if (ENV !== "production") {
1010

1111
export 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
};

src/helpers/cors.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const whitelist: RegExp[] = [
2+
/^https?:\/\/localhost:3000$/,
3+
/^https?:\/\/example\.com$/,
4+
/^https?:\/\/subdomain\.example\.com$/
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+
};

src/server.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { pinoHttp } from "pino-http";
33
import { config } from "./config.js";
44
import { routes } from "./routes/index.js";
55
import cors from "cors";
6+
import helmet from "helmet";
7+
import cookieParser from "cookie-parser";
8+
import { corsOptions } from "./helpers/cors.ts";
69

710
const { logger } = pinoHttp();
811

@@ -20,29 +23,40 @@ checkConfigIsValid();
2023

2124
const app = express();
2225

26+
//
27+
// Middleware
28+
//
29+
30+
// Logger
2331
app.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
3046
app.use(express.urlencoded({ extended: true }));
3147

3248
// parse application/json
3349
app.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
4252
app.use(cors(corsOptions));
53+
app.options("*", cors(corsOptions));
4354

55+
//
56+
// Routes
57+
//
4458
routes(app);
4559

4660
app.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
});

0 commit comments

Comments
 (0)