【发布时间】: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