Search code examples
node.jshttp-proxy

bypass nodejs express-http-proxy from some requests


I have an express-http-proxy used to redirect the request to another server. I would like to redirect some requests only, here below are the redirected requests code;

app.use('/api', proxy('abc.com', {
// preserveHostHdr: true,
https: isHttps,
proxyReqPathResolver: function (req) {
    return require('url').parse(req.url).path;
}}));

I tried

app.use('/', proxy('abc.com', {
// preserveHostHdr: true,
https: isHttps,
proxyReqPathResolver: function (req) {
    return require('url').parse(req.url).path;
}}));

after the first block to bypass any other requests but I found out that proxy keeps forwarding any request. is there a way to bypass the proxy and call some HTML files on the same host?


Solution

  • I found the solution as bellow; I must first declare a static path to express and all of the path content will be public. after that ı did the redirection.

    app.use('/api', proxy('abc.com', {
            // preserveHostHdr: true,
            https: isHttps,
                proxyReqPathResolver: function (req) {
                return require('url').parse(req.url).path;
            }
        }));
    
    app.use(express.static("./web"));
    app.get('/',function(req,res){
             res.sendFile(require('path').join(__dirname, ''));
      });