-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (36 loc) · 1.33 KB
/
Copy pathserver.js
File metadata and controls
43 lines (36 loc) · 1.33 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
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const cors = require('cors');
const app = express();
const PORT = 3000;
const uploadDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir);
const storage = multer.diskStorage({
destination: uploadDir,
filename: (req, file, cb) => cb(null, file.originalname)
});
const upload = multer({ storage });
app.use(cors());
app.use(express.json());
app.use(express.static(uploadDir));
app.use(express.static(path.join(__dirname, 'public')));
app.post('/upload', upload.single('file'), (req, res) => {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
res.json({ message: 'File uploaded successfully', filename: req.file.originalname });
});
app.get('/files', (req, res) => {
fs.readdir(uploadDir, (err, files) => {
if (err) return res.status(500).json({ error: 'Could not read upload directory' });
res.json(files);
});
});
app.get('/download/:filename', (req, res) => {
const filePath = path.join(uploadDir, req.params.filename);
if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'File not found' });
res.download(filePath);
});
app.listen(PORT, () => {
console.log(`🚀 FileShare portal running at http://localhost:${PORT}`);
});