-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
81 lines (70 loc) · 1.96 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import swaggerUi from "swagger-ui-express";
import swaggerJsdoc from "swagger-jsdoc";
import cors from "cors";
// import userRoutes from "./routes/userRoutes.js";
import appRoutes from "./src/routes/appRoutes.js";
dotenv.config();
const app = express();
const corsOptions = {
origin: "http://localhost:5173", // Allow only your frontend domain
methods: "GET,POST,PUT,DELETE",
allowedHeaders: "Content-Type,Authorization",
};
// Use CORS middleware
app.use(cors(corsOptions));
// Connect to DB
const PORT = process.env.PORT || 8000;
const MONGOURL = process.env.MONGO_URL;
mongoose
.connect(MONGOURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Database connected successfully.");
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((err) => {
console.error("Database connection error:", err);
});
// Swagger
const swaggerOptions = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "Express API with Swagger",
version: "1.0.0",
description:
"This is a simple CRUD API application made with Express and documented with Swagger",
},
servers: [
{
url: "https://testdb-fajc.onrender.com",
},
{
url: "http://localhost:8000",
},
],
},
// Update here to include the path to your controllers
apis: ["./src/routes/*.js", "./src/controllers/*.js"],
};
const specs = swaggerJsdoc(swaggerOptions);
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(specs, { explorer: true })
);
// app.use("/allusers", userRoutes);
app.use("/api", appRoutes);
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});