Node (Express JS) 中间件使用http-proxy-middleware代理客户端(可能是 Chrome 浏览器)和 jira 服务器之间的请求和响应。它还使用https-proxy-agent添加代理,因为主机服务器需要代理才能访问 Jira API。
请求标头已使用onProxyReq更新。它在 localhost 中完全可以正常工作,因为它不需要代理,但它会在服务器中引发错误“ERR_HTTP_HEADERS_SENT:Cannot set headers after they are sent to the client”。
代码实现如下
var express = require("express");
var proxy = require("http-proxy-middleware");
var HttpsProxyAgent = require("https-proxy-agent");
var router = express.Router();
// proxy to connect open network
var proxyServer = "http://myproxy.url:8080";
router.use(
"/jira",
proxy({
target: "https://myJira.url",
agent: new HttpsProxyAgent(proxyServer),
secure: false,
changeOrigin: true,
onProxyReq: function(proxyReq, req, res) {
proxyReq.setHeader("user-agent", "XXX");
proxyReq.setHeader("X-Atlassian-Token", "nocheck");
console.log(
"Printing Req HEADERS: " + JSON.stringify(proxyReq.getHeaders())
);
},
onProxyRes: function(proxyRes, req, res) {
proxyRes.headers["Access-Control-Allow-Origin"] = "*";
},
pathRewrite: {
"^/api/jira/": "/"
}
})
);
module.exports = router;
感谢任何帮助解决它。