-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (40 loc) · 1.35 KB
/
server.js
File metadata and controls
49 lines (40 loc) · 1.35 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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const app = express();
// Set up file storage
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadDir = 'uploads';
if (!fs.existsSync(uploadDir)){
fs.mkdirSync(uploadDir);
}
cb(null, uploadDir);
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}-${file.originalname}`);
}
});
const upload = multer({ storage: storage });
app.use(express.static('public')); // Serve static files
app.use(express.json()); // Parse JSON bodies
app.post('/upload', upload.array('mangaFiles'), (req, res) => {
const mangaTitle = req.body.mangaTitle;
const description = req.body.description;
const files = req.files;
if (!files || files.length === 0) {
return res.status(400).json({ success: false, message: 'No files uploaded.' });
}
const manga = {
title: mangaTitle,
description: description,
fileName: files[0].filename // Just showing the first image as a thumbnail
};
res.json({ success: true, manga: manga });
});
// Serve the uploads folder
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.listen(3000, () => {
console.log('Server is running on port 3000');
});