-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (43 loc) · 1.42 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
44
45
46
47
48
49
50
51
52
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const port = 3000;
// Set up multer for file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
// Serve HTML and CSS
app.use(express.static(path.join(__dirname, 'public')));
// Define a route for the root path
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Handle file uploads
app.post('/upload', upload.single('file'), (req, res) => {
if (req.file) {
res.json({ filename: req.file.originalname });
} else {
res.status(400).json({ error: 'No file uploaded' });
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
app.get('/uploads/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(__dirname, 'uploads', filename);
// Use the res.download method to initiate a file download
res.download(filePath, filename, (err) => {
if (err) {
// Handle errors, for example, file not found
res.status(404).json({ error: 'File not found' });
}
});
});