|
1 | 1 | import express, { Request, Response, NextFunction } from "express";
|
2 | 2 | import cors from "cors";
|
| 3 | +import dotenv from "dotenv"; |
3 | 4 | import fs from "fs";
|
4 | 5 | import yaml from "yaml";
|
5 | 6 | import swaggerUi from "swagger-ui-express";
|
6 | 7 |
|
7 | 8 | import userRoutes from "./routes/user-routes.js";
|
8 | 9 | import authRoutes from "./routes/auth-routes.js";
|
9 | 10 |
|
| 11 | +dotenv.config(); |
| 12 | + |
10 | 13 | const file = fs.readFileSync("./swagger.yml", "utf-8");
|
11 | 14 | const swaggerDocument = yaml.parse(file);
|
| 15 | +const origin = process.env.ORIGINS |
| 16 | + ? process.env.ORIGINS.split(",") |
| 17 | + : ["http://localhost:5173", "http://127.0.0.1:5173"]; |
12 | 18 |
|
13 | 19 | const app = express();
|
14 | 20 |
|
15 | 21 | app.use(express.urlencoded({ extended: true }));
|
16 | 22 | app.use(express.json());
|
17 | 23 | app.use(
|
18 | 24 | cors({
|
19 |
| - origin: ["http://localhost:5173", "http://127.0.0.1:5173"], |
| 25 | + origin: origin, |
20 | 26 | credentials: true,
|
21 | 27 | })
|
22 | 28 | ); // config cors so that front-end can use
|
23 |
| -// app.options( |
24 |
| -// "*", |
25 |
| -// cors({ |
26 |
| -// origin: ["http://localhost:5173", "http://127.0.0.1:5173"], |
27 |
| -// credentials: true, |
28 |
| -// }) |
29 |
| -// ); |
| 29 | +app.options( |
| 30 | + "*", |
| 31 | + cors({ |
| 32 | + origin: ["http://localhost:5173", "http://127.0.0.1:5173"], |
| 33 | + credentials: true, |
| 34 | + }) |
| 35 | +); |
30 | 36 |
|
31 | 37 | // To handle CORS Errors
|
32 |
| -// app.use((req: Request, res: Response, next: NextFunction) => { |
33 |
| -// res.header("Access-Control-Allow-Origin", "*"); // "*" -> Allow all links to access |
34 |
| - |
35 |
| -// res.header( |
36 |
| -// "Access-Control-Allow-Headers", |
37 |
| -// "Origin, X-Requested-With, Content-Type, Accept, Authorization" |
38 |
| -// ); |
39 |
| - |
40 |
| -// // Browsers usually send this before PUT or POST Requests |
41 |
| -// if (req.method === "OPTIONS") { |
42 |
| -// res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH"); |
43 |
| -// return res.status(200).json({}); |
44 |
| -// } |
45 |
| - |
46 |
| -// // Continue Route Processing |
47 |
| -// next(); |
48 |
| -// }); |
| 38 | +app.use((req: Request, res: Response, next: NextFunction) => { |
| 39 | + res.header("Access-Control-Allow-Origin", req.headers.origin); // "*" -> Allow all links to access |
| 40 | + |
| 41 | + res.header( |
| 42 | + "Access-Control-Allow-Headers", |
| 43 | + "Origin, X-Requested-With, Content-Type, Accept, Authorization" |
| 44 | + ); |
| 45 | + |
| 46 | + // Browsers usually send this before PUT or POST Requests |
| 47 | + if (req.method === "OPTIONS") { |
| 48 | + res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH"); |
| 49 | + return res.status(200).json({}); |
| 50 | + } |
| 51 | + |
| 52 | + // Continue Route Processing |
| 53 | + next(); |
| 54 | +}); |
49 | 55 |
|
50 | 56 | app.use("/api/users", userRoutes);
|
51 | 57 | app.use("/api/auth", authRoutes);
|
|
0 commit comments