Express 为什么我所有的firebase云功能都超时了?

Express 为什么我所有的firebase云功能都超时了?,express,firebase,google-cloud-functions,Express,Firebase,Google Cloud Functions,我正在尝试使用firebase云函数创建外部json api的代理。但现在我只是想把一切都安排好 我写了这个函数: exports.helloWorld = functions.https.onRequest((request, response) => { request.get('http://www.google.com', function (error, response, body) { if (!error && response.statusCo

我正在尝试使用firebase云函数创建外部json api的代理。但现在我只是想把一切都安排好

我写了这个函数:

exports.helloWorld = functions.https.onRequest((request, response) => {
  request.get('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body) // Print the google web page.
    }
  })
});
然后我运行firebase函数模拟器并运行

curl http://localhost:5000/<project-id>/us-central1/helloWorld
我不确定我做错了什么

编辑

此功能工作正常:

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send('test');
})

对于云函数,HTTPS类型的函数必须向客户端写入一个结果,以指示函数已完成执行。在编写结果之前,假定函数仍在运行异步工作

因此,当您的请求完成时,您应该发送一些响应,即使它是空的。不幸的是,您已经用另一个对象隐藏了主
响应
对象,因此您可能应该重命名其中一个:

exports.helloWorld = functions.https.onRequest((request, response) => {
  request.get('http://www.google.com', function (error, res, body) {
    if (!error && res.statusCode == 200) {
      console.log(body) // Print the google web page.
    }
    return response.send("") // this terminates the function
  })
})

HTTPS函数只有在您在响应中发送内容后才能完成。下面是一个示例,它将代理请求中的内容作为输出进行管道传输(我必须更改变量的名称以避免阴影:

exports.helloWorld = functions.https.onRequest((req, res) => {
  request.get('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      return res.send(body) // Print the google web page.
    }
    return res.send('ERROR: ' + error.message);
  })
});

我复制并粘贴了你的确切函数,但它仍然超时。但我知道firebase设置是正确的,因为如果我只发送一个字符串响应,它就会正常工作。请参阅我的编辑。我在发送之前放置了一个返回值-这应该可以解决问题。我很抱歉,我很感谢你的帮助,但它仍然超时。我想这可能是另一个shadowing问题,但我也尝试过它的变体。除了google和类似的东西之外,我还尝试过其他URL。是否有更好的方法来编写函数?请研究使用请求承诺库来使用您现在使用的请求库的承诺版本。承诺通常比传递回调函数更容易使用还有,记住你的项目必须在blaze计划中才能发出http请求。我在blaze上。我会试试的。谢谢。
exports.helloWorld = functions.https.onRequest((req, res) => {
  request.get('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      return res.send(body) // Print the google web page.
    }
    return res.send('ERROR: ' + error.message);
  })
});