-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (31 loc) · 1.19 KB
/
index.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
const express = require('express');
const multer = require('multer');
const videoRoutes = require('./routes/video');
const authMiddleware = require('./middlewares/authMiddleware');
require('dotenv').config();
const app = express();
// very important to parse the incoming request body to json
app.use(express.json());
const PORT = process.env.PORT || 3000;
// global authmiddleware which will authenticate all the routes
app.use(authMiddleware);
// Use video routes
app.use("/api/videos", videoRoutes);
// Global error handling middleware for multer errors
app.use((err, req, res, next) => {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
if (err.code === "LIMIT_FILE_SIZE") {
return res.status(400).json({ error: "File size exceeds the maximum limit." });
}
} else if (err) {
// An unknown error occurred when uploading.
console.error(err.message); // Log for debug
return res.status(500).json({ error: "An error occurred", details: err.message });
}
// If this middleware is hit without an error, pass to the next middleware
next();
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});