-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (30 loc) · 954 Bytes
/
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
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
app.use(cors());
app.get('/redirect', async (req, res) => {
const url = req.query.url;
if (!url) {
return res.status(400).send('URL is required');
}
try {
// Follow all redirects manually
let finalUrl = url;
let response = await axios.get(finalUrl, {
maxRedirects: 5, // Follow up to 5 redirects
validateStatus: (status) => status >= 200 && status < 400,
});
if (response.request.res.responseUrl) {
finalUrl = response.request.res.responseUrl;
}
res.send(finalUrl);
} catch (error) {
console.error('Error fetching redirected URL:', error.message);
res.status(500).send('Error fetching the redirected URL');
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Proxy running on http://localhost:${PORT}`);
});