【问题标题】:"ERR_HTTP_HEADERS_SENT:Cannot set headers after they are sent to the client" while http-proxy-middleware combined with https-proxy-agent“ERR_HTTP_HEADERS_SENT:发送到客户端后无法设置标头”,而 http-proxy-middleware 与 https-proxy-agent 结合使用
【发布时间】:2020-04-21 13:20:53
【问题描述】:

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;

感谢任何帮助解决它。

【问题讨论】:

    标签: node.js express http-proxy-middleware https-proxy-agent


    【解决方案1】:

    http-proxy-middleware 内部使用 http-proxy 模块。

    http-proxy 模块中,有一个关于使用proxyReq(或onProxyReq 用于http-proxy-middleware)作为公司代理的未解决问题:https://github.com/http-party/node-http-proxy/issues/1287

    b44x 找到的解决方法是使用http-proxy 模块中的proxy.web 低级方法。

    我遇到了同样的问题,我是这样解决的:

    const http = require('http');
    const httpProxy = require('http-proxy');
    
    
    // define an agent to proxy request to corporate proxy
    const proxyAgent = require('proxy-agent');
    // get values from environnement variables and avoid hard-coded values
    const corporateProxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY || '';
    const agent = corporateProxyUrl ? new proxyAgent(corporateProxyUrl) : false;
    
    
    const myLocalProxy = httpProxy.createProxyServer({});
    
    const myServer = http.createServer(function(req, res) {
    
      // change request before it's being sent to target
      delete req.headers.origin;
    
      myLocalProxy.web(req, res, {
        // instruct 'http-proxy' to forward this request to 'target'
        // using 'agent' to pass through corporate proxy
        target: 'https://www.example.com',
        changeOrigin: true,
        agent: agent,
        toProxy: true,
      });
    
    });
    
    myServer.listen(8003);
    console.log('[DEMO] Server: listening on port 8003');
    

    【讨论】:

    • 感谢您指出开放的错误,所以,我不能在 express 的 router.use() 中使用这个代理吗?
    猜你喜欢
    • 2019-02-06
    • 2020-05-20
    • 2019-06-15
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2020-06-21
    • 2021-07-21
    相关资源
    最近更新 更多