首页 > 解决方案 > Firebase Cloud Functions + Express 示例上的“无法获取”错误

问题描述

我是 firebase 的新手,并试图从下面的视频中制作简单的 Cloud Functions + Express 示例来工作。
https://www.youtube.com/watch?v=LOeioOKUKI8

当我尝试从http://localhost:5000/timestamp提供我的 index.js 时,我收到以下错误。

无法获取 /{my-project-id}/us-central1/app/timestamp

在我的终端中,我得到以下输出。

⚠ 创建了默认的“firebase-admin”实例!i 函数:在 ~1s [hosting]
内完成“app ”

但是如果我部署,它会按预期工作,并且会显示我的时间戳。

我当前的代码如下。

index.js

var admin = require("firebase-admin");
admin.initializeApp();

const functions = require('firebase-functions');
const express = require('express');

const app = express();

app.get('/timestamp', (request, response) => {
  response.send(`${Date.now()}`);
});

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

firebase.json

{
  "hosting": {
    "public": "public",
    "rewrites": [{
      "source": "/timestamp",
      "function": "app"
    }],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

如果我将 index.js 的一部分重写为类似的东西,

app.get('/{my-project-id}/us-central1/app/timestamp', (request, response) => {
  response.send(`${Date.now()}`);
});

当我访问http://localhost:5000/timestamp时,它将显示我的时间戳。
有人知道为什么会这样吗?

标签: node.jsfirebaseexpressgoogle-cloud-functionsfirebase-hosting

解决方案


在我这边,我试图开发一个带有 firebase 云功能的 Rest API。我有同样的错误。如果我通过“Firebase deploy”推送我的代码 Firebase 服务器,它正在运行我想要的。但是,当我通过“firebase serve --only 功能,托管”命令在本地服务器上运行时,总会出现无法获取且无法运行的错误。我在这里尝试了您的代码和相同的代码。我为此找到了一个非常简单的解决方案并站在我这边。可以在本地试穿吗

 app.get('*/timestamp', (request, response) => {
   response.send(`${Date.now()}`);
 });

只需在您的路径前添加 *。

更新 :

'firebase-tools' 已更新。如果您将 6.9.2 的错误版本更新到 6.10.0,则问题已得到修复。

要更新最新的 firebase-tools:

npm i -g firebase-tools

推荐阅读