5

I'm using proxy.web to forward client requests. When destination server is up, my code works as expected. When destination server is down, ECONNREFUSED error is catch and printed to console.log. I would like to send that error back to the client, and tried using the sample provided here. Unfortunately, the error response does not arrive to the client (tried both chrome and firefox). Please find below code. Why does the response not sent to the client ?

var proxyServer = http.createServer(function(req, res) {

if(req.path === 'forbidden') {
    return res.end('nope');
}

var url_parts = url.parse(req.url);
var extname = path.extname(url_parts.pathname);

    if (extname || url_parts.pathname.length <= 1){
        proxy.web(req, res, {
            target: 'http://localhost:'+config.fileServer.port
        });
    }
    else{
        proxy.web(req, res, {
            target: config.recognitionServer.url
        }, function(e) {
            console.log(e.message);
            if (!res.headersSent) {
                res.writeHead(500, { 'content-type': 'application/json' });
            }
            res.end(JSON.stringify({ error: 'proxy_error', 
                   reason: e.message         
            }));
        });
    }

 }).listen(config.proxyServer.port, function () {
    console.log('Proxy server is listening on port ' 
    + config.proxyServer.port);
 });

2 Answers 2

3

A good approach is this:

return res.status(500).send({
    error: true,
    message: 'your-error-message'
});

Your code rewritten:

proxy.web(req, res, {
    target: config.recognitionServer.url
}, function (e) {
    console.log(e.message);
    return res.status(500).send({
       error: true,
       message: e.message
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank for your answer, Michelem. Using res.status(500).send() is not possible as the res object does not expose send() and status() methods. I tried using other methods such as end(), write(), unfortunately with no success. The server sends an error response to the client, but client never gets it. The only way to wake the client is to close the server application. But this is not a good practice.
I'm using express 4.x. The "res" object at the event handler scope does not have send() and status() methods. In outer scope it does have them. Did you try running the code you have pasted above with http-proxy module ?
If I use req.pipe() instead of proxy.web, it is possible to call res.status(500).send() inside the event handler, but the response does not reach the client either.
1

Problem was solved on client side :) Client code is JS using XMLHttpRequest (tested on FF and Chrome). The error response arrives to "onload" event handler, not to "onerror". The "onload" handler function needs to check response status. If error status (500), continue with error handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.