【问题标题】:Using Express in firebase calls function but creates a timeout在 firebase 调用函数中使用 Express 但会产生超时
【发布时间】:2020-06-08 05:09:48
【问题描述】:

我有点不知所措。

我有以下 node.js 设置:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cookieParser = require('cookie-parser');
var https = require('https');

admin.initializeApp();

let app = express();

app.use(cookieParser);


// Set/Get Cookie

app.get('/setuser', (req, res) => {
    console.log('**************SET USER');
    console.log('**************req:');
    console.log(req);

    console.log('**************res:');
    console.log(res);
    res.send('setting user');
});

app.get('/getuser', (req, res) => {

    console.log('**************GET USER');
    console.log('**************req:');
    console.log(req);

    console.log('**************res:');
    console.log(res);
    res.send('getting user');
    //res.send(req.cookies);
});

exports.main = functions.https.onRequest(app);

在我的 firebase.json 文件中:

"hosting": {
    "site": "sso-login",
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/main/**",
        "function": "main"
      },
      {
        "source": "**",
        "destination": "index.html"
      }
    ]
  }

我在 Firebase 模拟器中本地运行整个应用程序。

现在,当我打电话时

https://localhost:5100/main/getuser Or https://localhost:5100/main/getuser/

模拟器的控制台显示Beginning execution of "main" - 然后什么也没有发生。

如果我从 firebase.json 中删除重写,我会得到一个简单的 404 错误。

如何使我的 setuser/getuser-functions 工作?

【问题讨论】:

  • 您是否尝试过删除重写并向localhost:5100/getuser 提出请求?
  • 是的,我做到了。然后我得到一个 404
  • 如果模拟器似乎不适用于非常清晰和简单的用例,您可以在项目 GitHub 上发布问题。确保您的 Firebase CLI 是最新的,并包含有关如何复制的完整说明。 github.com/firebase/firebase-tools

标签: node.js firebase google-cloud-functions firebase-cli


【解决方案1】:

您必须在 firebase HTTP 函数中发送响应。 模拟器卡住可能是因为对响应行的评论。 您可以在文档中阅读 https://firebase.google.com/docs/functions/http-events

【讨论】:

  • 嗯,我也试过了。 -> res.send('响应'); - 模拟器仍然超时。
  • 这似乎根本不是问题。显然有一行代码可以在两个路由中发送响应。
  • @DougStevenson - 我编辑了我的问题。该行之前已被注释掉。但取消注释并没有帮助。
猜你喜欢
  • 2019-07-04
  • 2019-09-03
  • 2016-02-24
  • 2019-11-28
  • 1970-01-01
  • 2018-10-31
  • 2016-02-03
  • 1970-01-01
  • 2020-11-05
相关资源
最近更新 更多