Create proxy.js
Browse files
proxy.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const PORT = process.env.PORT || 3000;
|
| 6 |
+
const TARGET_URL = process.env.TARGET_URL || 'https://domain.com';
|
| 7 |
+
|
| 8 |
+
// 代理所有请求到目标URL
|
| 9 |
+
app.use('/', createProxyMiddleware({
|
| 10 |
+
target: TARGET_URL,
|
| 11 |
+
changeOrigin: true,
|
| 12 |
+
logLevel: 'info',
|
| 13 |
+
onProxyReq: (proxyReq, req, res) => {
|
| 14 |
+
// 可选:添加或修改请求头
|
| 15 |
+
proxyReq.setHeader('X-Proxy-Server', 'Node-Proxy');
|
| 16 |
+
},
|
| 17 |
+
onError: (err, req, res) => {
|
| 18 |
+
console.error('Proxy error:', err);
|
| 19 |
+
res.status(500).send('Proxy error occurred');
|
| 20 |
+
}
|
| 21 |
+
}));
|
| 22 |
+
|
| 23 |
+
app.listen(PORT, () => {
|
| 24 |
+
console.log(`Proxy server running on http://localhost:${PORT}`);
|
| 25 |
+
console.log(`Proxying requests to: ${TARGET_URL}`);
|
| 26 |
+
});
|