proxy-server.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // proxy-server.js
  2. const express = require('express')
  3. const { createProxyMiddleware } = require('http-proxy-middleware')
  4. const app = express()
  5. // n8n 代理配置(转发所有请求到 n8n 服务器)
  6. const n8nProxy = createProxyMiddleware('**', { // 匹配所有路径(包括 /home/workflows)
  7. target: 'https://n8n.citupro.com', // 替换为真实的 n8n 服务器地址
  8. changeOrigin: true,
  9. ws: true, // 支持 WebSocket(n8n 实时通信需要)
  10. onProxyReq: (proxyReq, req, res) => {
  11. // 可选:添加日志,查看转发的请求
  12. console.log(`转发请求:${req.method} ${req.originalUrl} -> ${proxyReq.path}`)
  13. },
  14. onProxyRes: (proxyRes, req, res) => {
  15. // 关键:移除 n8n 服务器返回的限制头
  16. delete proxyRes.headers['x-frame-options']
  17. delete proxyRes.headers['content-security-policy']
  18. delete proxyRes.headers['x-xss-protection'] // 可选:避免 XSS 限制影响开发
  19. },
  20. onError: (err, req, res) => {
  21. // 错误处理:方便排查转发失败问题
  22. console.error(`代理错误:${err.message}`)
  23. res.status(500).send('代理服务器错误,请检查配置')
  24. }
  25. })
  26. app.use(n8nProxy)
  27. const PORT = 3001
  28. app.listen(PORT, () => {
  29. console.log(`代理服务器运行在 http://localhost:${PORT}`)
  30. })