Firebase Cloud functions: ECONNREFUSED

限于喜欢 提交于 2021-02-15 05:10:15

问题


I am trying the Optimizing Networking of firebase cloud functions like here with Typescript

const http = require('http');
const functions = require('firebase-functions');
const agent = new http.Agent({keepAlive: true});

export const getXXX = functions.https.onRequest((request, response) => {
    const req = http.request({
        host: 'localhost',
        port: 443,
        path: '',
        method: 'GET',
        agent: agent,
    }, res => {
        let rawData = '';
        res.setEncoding('utf8');
        res.on('data', chunk => { rawData += chunk; });
        res.on('end', () => {
            response.status(200).send(`Data: ${rawData}`);
        });
    });
    req.on('error', e => {
        response.status(500).send(`Error: ${e.message}`);
    });
    req.end();
});

but I keep getting

error: connect ECONNREFUSED 127.0.0.1:443

I am not very familiar with TypeScript and js so please help me.
Another question when is res.on 'Data' gets triggered ?


回答1:


You can't access anything on "localhost" (127.0.0.1) in Cloud Functions. I suspect that you meant to put a different host in there, and ensure that your project is on the Blaze plan to enable outgoing connections to services not fully controlled by Google.




回答2:


Turns out I need to be on a paid plan in order to make external HTTP requests from inside my function.




回答3:


You can run Cloud Functions on localhost. All you need to do is run a local emulator of the Cloud services. Which Google has provided! It's a really awesome tool and a great setup!

Follow these steps for the Firebase tool suite: https://firebase.google.com/docs/functions/local-emulator

Follow these steps for the Cloud tool suite: https://cloud.google.com/functions/docs/emulator

They are pretty much similar.

You do not need Blaze plan, you can use the "pay as you go" plan, which includes the free tier quota. "Free usage from Spark plan included*" https://firebase.google.com/pricing



来源:https://stackoverflow.com/questions/50955122/firebase-cloud-functions-econnrefused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!