-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
38 lines (31 loc) · 1.05 KB
/
index.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
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
const port = 3000;
const downloadsPath = 'C:/Users/User/Downloads'; // Replace this with your actual Downloads folder path
const whitelist = ['https://youtube.com', 'http://localhost:3000']
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
app.use(cors(corsOptions));
app.use(express.static(downloadsPath));
app.get('/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(downloadsPath, filename);
res.sendFile(filePath, err => {
if (err) {
console.error(`Error sending file: ${err.message}`);
res.status(err.status || 500).send('Internal Server Error');
}
});
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});