Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 843388c

Browse files
committed
fix: move via header to proxyRes event
1 parent 93be44d commit 843388c

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

src/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export default function(fn, opts={}) {
3535

3636
const proxy = httpProxy.createProxyServer({ target });
3737

38+
proxy.on("proxyRes", function(proxyRes, req, res) {
39+
const existing = res.getHeader("Via");
40+
const viaheader = `${existing ? existing + ", " : ""}${req.httpVersion} ${via} (${name}/${version})`;
41+
res.setHeader("Via", viaheader);
42+
});
43+
3844
return async function(req, res, next) {
3945
try {
4046
// hijack the root response and inject proxy information
@@ -51,20 +57,6 @@ export default function(fn, opts={}) {
5157
if (secret) req.headers[headerFields.token] = signRequest(name, secret);
5258
}
5359

54-
// attach Via header on response
55-
// do this last in case there was an error
56-
if (via) {
57-
const writeHead = res.writeHead;
58-
res.writeHead = function(code, headers) {
59-
const existing = res.getHeader("Via");
60-
const viaheader = `${existing ? existing + ", " : ""}${req.httpVersion} ${via} (${name}/${version})`;
61-
res.setHeader("Via", viaheader);
62-
if (headers) headers["Via"] = viaheader;
63-
64-
return writeHead.apply(res, arguments);
65-
};
66-
}
67-
6860
proxy.web(req, res);
6961
} catch(e) {
7062
if (next) next(e);

0 commit comments

Comments
 (0)