Error while requesting user app before its up
Asked Answered
C

1

11

I use node-http proxy module to run application with reverse proxy which is working as expected, in some cases user want to run application immediately which the status of it in progress (the app is not up yet) and it can take about 3-15 sec until the app is up and running; in this case user will get error from the proxy.

proxy.web(req, res, {
    target: 'http://' + hostname + ':' + Port
     console.log("App------------->Proxy App" );
});

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    console.log("App------------->Proxy Request" );
});

  proxy.on('error', function (err, req, res) {
     console.log("App------------->Proxy Error" );
      res.end('Something went wrong');
    });
    
    // Listen for the `proxyRes` event on `proxy`.
    proxy.on('proxyRes', function (proxyRes, req, res) {
      console.log("App------------->Proxy Response" )
        var respState = res.statusCode
    });

In case of error the stack in the log is like

  1. Proxy app
  2. Proxy Request
  3. Proxy Error

In this case user will run the app url in the browser and first will get the error and after few seconds when he refresh the browser the app will run OK. Any suggestion how to solve this issue ?

I thought about building some API which examine the proxyRes status(like call it every 1 sec and see if the response is 200 and not send the error before like "check it with timeout" and if after 10 sec there is not response maybe to send the error but not sure how to do it and if its good approach... any idea? or maybe via webSoket but not sure how to do that ...

This is the open source Im using https://github.com/nodejitsu/node-http-proxy

Caul answered 14/1, 2016 at 11:56 Comment(3)
I don't think node-proxy can achieve that since it only copies request to target & pipes back the result. and if there is no response from target. it exactly sends back the http status or unreacable code. in this case you have to build your own proxy function which repeat the request until it receives correct status code such as 200 or 30X. i hope this helps.Disillusionize
@Disillusionize - Thanks but its too late to change it :( ,any other idea how can I maybe repeat it with JS code/events ?Caul
If the user is going to use a web browser; when proxy fails you can response HTML and client side JavaScritp with a beautifull animated GIF of "Loading" and some asyncronous request to your server every 1 or 2 seconds. Put a counter in the client and make client side script show a rrror when you think retries are too much.Steinke
K
1

Try to set proxyTimeout in options. This flag is not documented but it is used in the code:

https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L119

var proxy = httpProxy.createProxyServer({proxyTimeout:3000}) // timeout 3 seconds
Kelsey answered 19/1, 2016 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.