proxy.js 555 B

1234567891011121314151617181920
  1. const express = require('express');
  2. const { createProxyMiddleware } = require('http-proxy-middleware');
  3. const app = express();
  4. // 反向代理配置
  5. const proxyOptions = {
  6. target: 'https://api.openai.com', // OpenAI API 服务器地址
  7. changeOrigin: true, // 修改请求头以适应目标服务器
  8. pathRewrite: { '^/api': '' }
  9. };
  10. // 设置代理
  11. app.use('/api', createProxyMiddleware(proxyOptions));
  12. // 启动服务器
  13. const PORT = process.env.PORT || 3001;
  14. app.listen(PORT, () => {
  15. console.log(`Server is running on port ${PORT}`);
  16. });