Express HTTP Proxy Error Handling

I set up a local proxy using node, express and express-http-proxy, which redirects requests to a remote server, accessible only through a VPN. This works well when connecting a VPN. However, when there is no vpn connection, I get this error:

    _http_outgoing.js: 346
        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: 346: 11)
        at /path/to/my/project/proxy/node_modules/express/lib/application.js:152:9
        at next (/path/to/my/project/proxy/node_modules/express/lib/router/index.js:150:14)
        at SendStream.error (/path/to/my/project/proxy/node_modules/serve-static/index.js:74:37)
        at emitOne (events.js: 77: 13)
        at SendStream.emit (events.js: 169: 7)
        at SendStream.error (/path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.js:147:51)
        at SendStream.onStatError (/path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.js:248:48)
        at /path/to/my/project/proxy/node_modules/serve-static/node_modules/send/lib/send.jshaps20:26
        at FSReqWrap.oncomplete (fs.js: 82: 15)

My proxy setting:

    app.use ('/ the / api', proxy ('the.domain.behind.vpn: 8026', {
        timeout: 4000,

        forwardPath: function (req, res) {
            console.log (nodeUrl.parse (req.url) .path);

            return "/ the / api" + nodeUrl.parse (req.url) .path;
        },

        intercept: function (rsp, data, req, res, callback) {
            data = data.toString("utf8").replace(/http\:\/\/the\.domain\.behind\.vpn\:8026/g, "http://localhost:61000");
            callback(null, data);
        },

        decorateRequest : function(proxyReq, originalReq){
            if(proxyReq.method === "HEAD"){
                proxyReq.method = "GET";
            }

            return proxyReq;
        }
    }));

?

+4

Source: https://habr.com/ru/post/1654565/


All Articles