-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (25 loc) · 1.03 KB
/
index.js
File metadata and controls
31 lines (25 loc) · 1.03 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
import express from 'express';
import fetch from 'node-fetch';
import cors from 'cors'; // Import the cors middleware
const app = express();
const PORT = process.env.PORT || 3000;
// Use cors middleware to allow all origins
app.use(cors());
app.use('/proxy', async (req, res) => {
const url = req.query.url; // Get the URL from the query parameter
try {
const response = await fetch(url);
const data = await response.json();
// Set CORS headers manually if needed
res.header('Access-Control-Allow-Origin', '*'); // Allow all origins
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); // Allow methods
res.header('Access-Control-Allow-Headers', 'Content-Type'); // Allow headers
res.json(data); // Send back the data
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).send('Error fetching data');
}
});
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});