Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error response not sent to client #844

Open
oprince opened this issue Jun 29, 2015 · 4 comments
Open

Error response not sent to client #844

oprince opened this issue Jun 29, 2015 · 4 comments

Comments

@oprince
Copy link

oprince commented Jun 29, 2015

Hi

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);
});
@IGZjaviernieto
Copy link

+1

1 similar comment
@igzdavidsuarez
Copy link

+1

@acanimal
Copy link

I'm using http-proxy as a middleware and works fine returning any kind of error: connect ECONNREFUSED, getaddrinfo ENOTFOUND, etc.

    // Proxy request to the provider
    proxy.web(req, res, {
      target: provider.target
    }, function(err) {
      // If there is any proxy error return the error.
      next(err);
    });

Your code at the else seems fine maybe your request is entering in the if part.

Cheers.

@oprince
Copy link
Author

oprince commented Jul 19, 2015

Hi acanimal,
"If" part is reached, and the error is printed to the console log. I have found a partial solution on client side, it is documented in: http://stackoverflow.com/questions/31260825/node-js-http-proxy-error-response-not-sent-to-client.
It works well once or twice while listening server is up. But socket gets corrupted and server should be restarted to recover.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants