| |
| import express from 'express'; |
| import cors from 'cors'; |
| import fetch from 'node-fetch'; |
|
|
| const app = express(); |
| const PORT = process.env.PORT || 7860; |
|
|
| |
| const TARGET_DOMAIN = "https://aliyun.zaiwen.top"; |
|
|
| |
| app.use(express.json()); |
| app.use(cors({ |
| origin: '*', |
| methods: ['POST'], |
| allowedHeaders: ['Content-Type', 'Authorization'] |
| })); |
|
|
| |
| app.all('*', (req, res, next) => { |
| if (req.method !== 'POST') { |
| return res.status(405).send('Method Not Allowed'); |
| } |
| next(); |
| }); |
|
|
| |
| 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); |
|
|
| |
| response.headers.forEach((value, key) => { |
| if (key.toLowerCase() === 'content-length') return; |
| 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'); |
| } |
| }); |
|
|
| |
| app.listen(PORT, () => { |
| console.log(`Server running on port ${PORT}`); |
| }); |
|
|
| |
| export default app; |
|
|