title: Firebase Cloud functions: ECONNREFUSED
body: <p>I am trying the Optimizing Networking of firebase cloud functions like <a href="https://firebase.google.com/docs/functions/networking" rel="nofollow noreferrer">here</a> with Typescript</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const http = require('http');
  const functions = require('firebase-functions');
  const agent = new http.Agent({keepAlive: true});
  
  export const getXXX = functions.https.onRequest((request, response) =&gt; {
      const req = http.request({
          host: 'localhost',
          port: 443,
          path: '',
          method: 'GET',
          agent: agent,
      }, res =&gt; {
          let rawData = '';
          res.setEncoding('utf8');
          res.on('data', chunk =&gt; { rawData += chunk; });
          res.on('end', () =&gt; {
              response.status(200).send(`Data: ${rawData}`);
          });
      });
      req.on('error', e =&gt; {
          response.status(500).send(`Error: ${e.message}`);
      });
      req.end();
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>but I keep getting </p>
  
  <blockquote>
    <p>error: connect ECONNREFUSED 127.0.0.1:443</p>
  </blockquote>
  
  <p>I am not very familiar with TypeScript and js so please help me.
  <br>
  Another question when is res.on 'Data' gets triggered ?</p>