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()
})
但是没有用。帮助!!!
答案 0 :(得分:0)
只需戴上index.js
require('https').globalAgent.keepAlive = true;