The Node (Express JS) middle-ware uses http-proxy-middleware to proxy requests and responses between client (probably Chrome browser) and jira server. It also uses https-proxy-agent to add proxy agent as the host server requires proxy to access Jira APIs.
The request headers have been updated using onProxyReq. It completely works fine in localhost as it doesn't require proxy agent but it throws an error "ERR_HTTP_HEADERS_SENT:Cannot set headers after they are sent to the client" in server.
The code implementation is below
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;
Appreciate any help to resolve it.