3

I am using npm request module to forward an incoming request to another server as:

app.get("/somepath", function(req, res) {
    var url = proxySetting.global.url + req.url;
    req.pipe(request(url)).pipe(res);
});

Here: proxySetting.global.url == http://localhost:4000

Now when i forward incoming request like this to target server, if the target server(localhost:4000) is down or the request is hanged on target server.

There will be an error like ECONNREFUSED or hangup error.

Tried catching these error using domain module like below

var d = domain.create();
d.on("error", function(err) {
    console.log("Error occoured while forwarding request");
    console.log(err);
    res.status(500).send("Error occoured while forwarding request");
});
d.run(function() {
    req.pipe(request(url)).pipe(res);
});

Tried to catch error event in several combination

    var request = require("request");

module.exports = function(proxySetting) {
    return function(req, res, next) {
        var url = proxySetting.global.url + req.url;
        url = url.replace(/([^:]\/)\/+/g, "$1") // replacing multiple slashes with one
        console.log("Forwarding request to: " + url);

        function errorHandler(err) {
            console.log("Error occoured while forwarding request");
            console.log(err);
            res.status(500).send("Error occoured while forwarding request");
        }
        req.on("error", errorHandler);
        res.on("error", errorHandler);
        req.pipe(request(url).on("error",errorHandler)).pipe(res);
    };
};

but still the exception is thrown to the process and server crashed One way i am doing now is

process.on('uncaughtException', function(err) {
  console.log("Some unhandled error occoured");
  console.log(err);
  console.log("Stopping server");
  process.exit(1);
});

But i think catch uncaughtException and handle is not a proper solution

Mayank Kansal
  • 71
  • 1
  • 8
  • I don't have idea about nodeJS error handling but please look at this, http://stackoverflow.com/questions/25846475/node-js-handling-tcp-socket-error-econnrefused. Is this you are looking for? – Amit Sadafule Oct 10 '16 at 08:10
  • it is the partial solution to the problem, as per the solution they asked to check the connection and then forward request to the remote server. but there is another case when the remote server is up but the request get hang on the remote server and we will get a hang-up error that will kill the proxy server process. – Mayank Kansal Oct 10 '16 at 08:44

1 Answers1

1

It seems, you are not acting on response object in "errorHandler", if you see your code block (given below), res is out of scope.

function errorHandler(err) {
    console.log("Error occoured while forwarding request");
    console.log(err);
    res.status(500).send("Error occoured while forwarding request"); 
} 
req.on("error", errorHandler); 
res.on("error", errorHandler); 
req.pipe(request(url).on("error", errorHandler)) .pipe(res);

But, if you act on response object in error handle, there will not be any uncaughtException.

I have created a solution, if proxy server is down, it will act on response object on error handle, thus it will not hit uncaughtException event.

Here is the solution.

var response ;

app.get('/', function(req, res){
    var url = "http://localhost:3001" + req.url;
    response = res; // I am setting res to global variable here
    req.pipe(request(url).on("error",errorHandler)).pipe(res);

});


function errorHandler(err) {
    response.status(500).send("Error occoured while forwarding request");
}

process.on('uncaughtException', function(err) {
  console.log("Some unhandled error occoured");
  console.log(err);
  console.log("Stopping server");
  process.exit(1);
});

Here uncaughtException event won't be invoked, I have checked in solution to a repo, you can give it a try. Repo location - here .

Use "Run1.sh" for successful case, "Run2.sh" for proxy server down case.

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48