File size: 1,834 Bytes
82465f0
ab05c0e
 
 
 
 
82465f0
ab05c0e
82465f0
 
ab05c0e
 
 
 
 
82465f0
 
ab05c0e
 
82465f0
 
 
 
 
 
 
ab05c0e
82465f0
 
 
 
 
 
 
 
 
 
 
 
 
ab05c0e
82465f0
 
ab05c0e
82465f0
 
 
 
 
ab05c0e
82465f0
 
 
ab05c0e
82465f0
ab05c0e
82465f0
 
ab05c0e
 
 
82465f0
ab05c0e
82465f0
ab05c0e
 
82465f0
ab05c0e
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Import required packages
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';

const app = express();
const PORT = process.env.PORT || 7860;

// Target server
const TARGET_DOMAIN = "https://aliyun.zaiwen.top";

// Middleware setup
app.use(express.json());
app.use(cors({
  origin: '*',
  methods: ['POST'],
  allowedHeaders: ['Content-Type', 'Authorization']
}));

// Only allow POST requests
app.all('*', (req, res, next) => {
  if (req.method !== 'POST') {
    return res.status(405).send('Method Not Allowed');
  }
  next();
});

// Proxy handler
app.post('*', async (req, res) => {
  const targetUrl = `${TARGET_DOMAIN}${req.path}${req.url.includes('?') ? '?' + req.url.split('?')[1] : ''}`;
  
  try {
    const response = await fetch(targetUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        ...req.headers
      },
      body: JSON.stringify(req.body)
    });

    const body = await response.text(); // 保持原样,前端自己决定如何解析
    res.status(response.status);

    // 把目标服务器返回的 headers 也加回来(注意 CORS 要处理一下)
    response.headers.forEach((value, key) => {
      if (key.toLowerCase() === 'content-length') return; // 不转发 content-length
      res.setHeader(key, value);
    });

    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'POST');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

    return res.send(body);
  } catch (error) {
    console.error('Proxy Error:', error);
    return res.status(500).send('Proxy Server Error');
  }
});

// Start server
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

// For Huggingface deployment
export default app;