Download proxy.js from Rouder/proxy_db: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Rouder/proxy_db/resolve/main/proxy.js
- Command line
-
hf download hf://spaces/Rouder/proxy_db/proxy.js
-
curl -L -o proxy.js https://huggingface.co/spaces/Rouder/proxy_db/resolve/main/proxy.js
1.63 kB
| const express = require('express'); | |
| const { createProxyMiddleware } = require('http-proxy-middleware'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 3000; | |
| const TARGET_URL = process.env.TARGET_URL || 'https://domain.com'; | |
| // 允许所有跨域请求(生产环境应限制来源) | |
| app.use(cors({ | |
| origin: '*', | |
| methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], | |
| allowedHeaders: ['Content-Type', 'Authorization'] | |
| })); | |
| // 健康检查端点 | |
| app.get('/health', (req, res) => { | |
| res.status(200).json({ status: 'healthy' }); | |
| }); | |
| // 代理中间件配置 | |
| const proxyOptions = { | |
| target: TARGET_URL, | |
| changeOrigin: true, | |
| secure: false, // 仅开发使用,生产环境应为true | |
| logLevel: 'debug', | |
| xfwd: true, // 添加x-forwarded头 | |
| proxyTimeout: 10000, // 10秒超时 | |
| onProxyReq: (proxyReq, req, res) => { | |
| // 添加或修改请求头 | |
| proxyReq.setHeader('X-Forwarded-For', req.ip); | |
| proxyReq.setHeader('X-Proxy-Server', 'Node-Proxy-Service'); | |
| }, | |
| onError: (err, req, res) => { | |
| console.error('Proxy error:', err); | |
| res.status(502).json({ | |
| error: 'Bad Gateway', | |
| message: 'Proxy service temporarily unavailable', | |
| details: err.message | |
| }); | |
| } | |
| }; | |
| // 代理所有请求 | |
| app.use('/', createProxyMiddleware(proxyOptions)); | |
| // 全局错误处理 | |
| app.use((err, req, res, next) => { | |
| console.error('Global error:', err); | |
| res.status(500).json({ error: 'Internal Server Error' }); | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`Proxy server running on http://localhost:${PORT}`); | |
| console.log('CORS enabled for all origins'); | |
| }); |