Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const multer = require("multer");
|
| 3 |
+
const path = require("path");
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const PORT = 3000;
|
| 7 |
+
|
| 8 |
+
// Serve static files (frontend)
|
| 9 |
+
app.use(express.static(path.join(__dirname, "public")));
|
| 10 |
+
|
| 11 |
+
// Configure Multer for file storage
|
| 12 |
+
const storage = multer.diskStorage({
|
| 13 |
+
destination: (req, file, cb) => {
|
| 14 |
+
cb(null, "uploads/"); // Destination folder for uploaded files
|
| 15 |
+
},
|
| 16 |
+
filename: (req, file, cb) => {
|
| 17 |
+
cb(null, Date.now() + "-" + file.originalname); // Unique file name
|
| 18 |
+
},
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
const upload = multer({
|
| 22 |
+
storage: storage,
|
| 23 |
+
limits: { fileSize: 500 * 1024 * 1024 }, // 500MB limit
|
| 24 |
+
}).single("file");
|
| 25 |
+
|
| 26 |
+
// File upload endpoint
|
| 27 |
+
app.post("/upload", (req, res) => {
|
| 28 |
+
upload(req, res, (err) => {
|
| 29 |
+
if (err) {
|
| 30 |
+
if (err.code === "LIMIT_FILE_SIZE") {
|
| 31 |
+
return res.status(400).json({ error: "File size exceeds 500MB limit" });
|
| 32 |
+
}
|
| 33 |
+
return res.status(500).json({ error: "File upload failed" });
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// Generate a streaming link
|
| 37 |
+
const fileUrl = `${req.protocol}://${req.get("host")}/uploads/${req.file.filename}`;
|
| 38 |
+
res.json({ message: "File uploaded successfully", link: fileUrl });
|
| 39 |
+
});
|
| 40 |
+
});
|
| 41 |
+
|
| 42 |
+
// Serve uploaded files
|
| 43 |
+
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
|
| 44 |
+
|
| 45 |
+
// Start the server
|
| 46 |
+
app.listen(PORT, () => {
|
| 47 |
+
console.log(`Server is running at http://localhost:${PORT}`);
|
| 48 |
+
});
|