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