-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (81 loc) · 2.48 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
// Import dependencies
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import path from "path";
import { fileURLToPath } from "url";
import connectDB from "./config/db.js";
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
// Initialize environment variables
dotenv.config();
// Initialize Express app
const app = express();
// Connect to MongoDB
connectDB();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.json()); // Parse JSON data in requests
app.use(cors()); // Enable Cross-Origin Resource Sharing
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded data
// Determine __dirname for ES module compatibility
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Import routes
import movieRoutes from "./routes/movieRoutes.js";
import authRoutes from "./routes/authRoutes.js";
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "Movie Streaming API",
version: "1.0.0",
description: "API for movie streaming and downloading",
contact: {
name: "Developer Name",
email: "[email protected]",
},
},
servers: [
{
// url: process.env.BASE_URL || `http://localhost:${PORT}`,
url:
"https://movie-express-app.vercel.app/" ,
description: "Production server",
},
],
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: [{ bearerAuth: [] }],
},
apis: [path.join(__dirname, "routes/*.js")],
};
// Swagger setup
const swaggerDocs = swaggerJsdoc(swaggerOptions);
app.use("/api-docs", cors(), swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Use routes
app.use("/api/movies", movieRoutes);
app.use("/api/auth", authRoutes);
// Static files for uploads (if needed for local testing)
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
// Default Route
app.get("/", (req, res) => {
res.send("Welcome to the Movie Streaming App API!");
});
// Error Handling Middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: "Something went wrong!" });
});
// Set Port and Start Server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Swagger docs available at http://localhost:${PORT}/api-docs`);
});