-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
43 lines (37 loc) · 1.21 KB
/
server.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
const express = require('express');
const path = require('path');
const multer = require('multer');
const app = express();
// Set up storage engine for Multer
const storage = multer.diskStorage({
destination: './uploads/',
filename: (req, file, cb) => {
cb(null, file.originalname); // Save file with original name
}
});
// Initialize Multer
const upload = multer({ storage: storage });
// Serve static files from 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Handle file uploads via POST
app.post('/upload', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).json({ success: false, message: 'No file uploaded.' });
}
res.json({ success: true, filename: req.file.filename });
});
// Handle file download requests
app.get('/download', (req, res) => {
const fileName = req.query.file;
if (fileName) {
const filePath = path.join(__dirname, 'uploads', fileName);
res.download(filePath, fileName);
} else {
res.status(400).send('No file specified.');
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});