-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (31 loc) · 889 Bytes
/
server.js
File metadata and controls
35 lines (31 loc) · 889 Bytes
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
const express = require('express');
const cors = require('cors');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
// Enable CORS
app.use(cors({ origin: true }));
// Proxy logic to handle requests dynamically
app.use(
'/',
createProxyMiddleware({
changeOrigin: true,
router: (req) => {
const targetUrl = req.query.url;
if (!targetUrl) {
throw new Error("Missing 'url' query parameter");
}
return targetUrl; // Dynamically proxy based on the provided URL
},
pathRewrite: {
'^/': '', // Remove the prefix
},
onProxyReq: (proxyReq, req) => {
proxyReq.removeHeader('origin'); // Remove origin to avoid CORS issues
},
})
);
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});