-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (53 loc) · 1.66 KB
/
Copy pathserver.js
File metadata and controls
64 lines (53 loc) · 1.66 KB
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
const cors = require("cors");
const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
const config = require("./config/index.js");
const { clearTempDir } = require("./src/services/fileService.js");
dotenv.config();
const app = express();
const PORT = process.env.PORT || config.PORT;
const videoRoutes = require("./src/routes/videoRoutes.js");
const authRoutes = require("./src/routes/authRoutes.js");
// --- CORS ---
const allowedOrigins = [
"http://localhost:7777",
"http://localhost",
"https://sumi.stageddat.dev",
];
const corsOptions = {
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
console.log(`[CORS] Bloqueado origen: ${origin}`); // Log
callback(new Error("Not allowed by CORS"));
}
},
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.use("/", authRoutes);
app.use("/", videoRoutes);
app.use((req, res) => {
console.log(`[404] Route not found: ${req.method} ${req.path}`);
res.status(404).json({
error: "Route not found",
path: req.path,
method: req.method,
});
});
app.use((err, req, res, next) => {
console.error("Error:", err.message, err.stack);
res.status(500).json({ error: "Internal server error" });
});
app.listen(PORT, () => {
console.log(`🚀 Server running on port ${PORT}`);
clearTempDir();
});