【问题标题】:How can I maintain persist a Cloud Functions connection using ExpressJS如何使用 ExpressJS 保持持久的 Cloud Functions 连接
【发布时间】:2019-07-06 07:33:13
【问题描述】:
const http = require('http');
const functions = require('firebase-functions');
const agent = new http.Agent({keepAlive: true});

exports.function = functions.https.onRequest((request, response) => {
    req = http.request({
        host: '',
        port: 80,
        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();
});

这是来自Optimizing networking using Cloud Functions 的代码。如何更新它以使用 expressJS。

所以从代码中我看到诀窍是这一行const agent = new http.Agent({keepAlive: true}),然后在请求中agent: agent。

我尝试做类似的事情

const server = express()

server.use((req, res, next) => {
    req.agent = new http.Agent({keepAlive: true})
    next()
})

但是没有用。救命!!!

【问题讨论】:

    标签: node.js firebase express network-programming google-cloud-functions


    【解决方案1】:

    只要穿上 index.js

    require('https').globalAgent.keepAlive = true;
    

    【讨论】:

    • 嗨,欢迎来到 Stack Overflow。在发布指向您自己的内容的链接时,您必须表明它是您的。如果不这样做,则将其视为垃圾邮件。欲了解更多信息,请参阅How to not be a spammer。
    猜你喜欢
    • 2011-04-28
    • 2022-10-25
    • 2019-01-17
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多