2 years ago
#40199
Ty Savercool
How to rewrite returned paths when using a proxy
I have a use case where 2 applications are hosted on the same system, 1 externally (from the system) accessible, and 1 only accessible via localhost:8081.
I have tried http-proxy and http-proxy-middleware. In both cases I can get things partially working, but when I run into a redirect things usually break.
My internal app is mounted onto my external app via express like this:
app.use('/myproxy', createProxyMiddleware('/myproxy', {
target: "http://localhost:8081",
followRedirects: true,
changeOrigin: true,
// autoRewrite: true,
prependPath: true,
pathRewrite: { '^/myproxy' : '/' },
selfHandleResponse: true,
onProxyRes: responseHandler( async (responseBuffer, proxyRes, req, res) => {
let response = responseBuffer.toString('utf8');
response = response.replace("js/", "myproxy/js/")
return response
} )
}))
I have fiddled extensively with the various parameters like:
- pathRewrite: seems to affect the incoming request and I believe is set correctly
- followRedirects: works as expected, I need this enabled as my internal app does a redirect
- prependPath: This one I am a bit shaky on. I tried it both ways, can't figure out what it does
- pathRewrite: This I understand changes the path of the incoming request, this appears to be configured correctly
- onProxyRes: This was my last attempt, I thought I could just find all references in the text of the response and rewrite them, it does not appear to actually work as my references are unchanged in practice. selfHandleResponse was needed to use this.
My ultimate goal is that when I go to http://myServer/myproxy the responding documents that ask for things like /js/somescript.js are updated to /myproxy/js/somescript.js. Same for CSS and other static content.
Is this possible? Am I going about this the wrong way, maybe?
node.js
http-proxy
http-proxy-middleware
0 Answers
Your Answer