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

change response if proxy response is 5xx #968

Open
mhf-ir opened this issue Feb 27, 2016 · 7 comments
Open

change response if proxy response is 5xx #968

mhf-ir opened this issue Feb 27, 2016 · 7 comments

Comments

@mhf-ir
Copy link

mhf-ir commented Feb 27, 2016

I cant modify response if proxy response is 5xx.

proxy.on('proxyRes', function (proxyRes, req, res) {
  if (req.method === 'GET' && (proxyRes.statusCode >= 500 || proxyRes.statusCode <= 599)) {
    let cacheContent = getCacheContent(req);
    res.writeHead(cacheContent.statusCode, cacheContent.headers);
    res.write(cacheContent.buffer);
    res.end();
  }
});

It's give me an error:

_http_outgoing.js:344
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at /home/sweb/prj/node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js:86:13
    at Array.forEach (native)
    at Array.writeHeaders (/home/sweb/prj/node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js:84:35)
    at ClientRequest.<anonymous> (/home/sweb/prj/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:150:20)
@mhf-ir
Copy link
Author

mhf-ir commented Jun 11, 2016

I thinks it's must be major use cases for failover proxy server. why no attention ?

@bubenshchykov
Copy link

+1

3 similar comments
@orkideh
Copy link

orkideh commented Sep 28, 2016

+1

@kooshywoosh
Copy link

+1

@MrSpark2591
Copy link

+1

@clancytom
Copy link

I have the same problem

        // watch the event from the proxy , if the resule is 404 
        this.proxy.on('proxyRes',  (proxyRes, req, res) =>{
            if (proxyRes.statusCode == 404){
                let baseUrl : string = req.baseUrl;
                let regexp = /^(.*)\/u-dyn\/sync\.js\/?(?=\/|$)/i;
                if (baseUrl.match(regexp)){
                    this.logger.info(`the proxy is not avalible ,so will load the i18n Info from local`);
                    this.provideI18NInfo(res);
                }
            }
        });
       this.proxy.web(req, res, {target: this.proxyUrl, proxyTimeout : 2000},error =>{})

@slavab89
Copy link

Use the following method posted here Worked great for me!!

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

7 participants