node.js - 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时,它将显示我的时间戳。
有人知道为什么会这样吗?
解决方案
在我这边,我试图开发一个带有 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
推荐阅读
- python - 使用 Python 将 csv 加载到 Oracle 中
- installation - bootstrap 安装指南 - 极限初学者
- android - textDirection 没有使edittext从右到左
- python - 将数字相加 - 可以打印 - 在 Python 中
- google-data-studio - 如何将 DataStudio 过滤器控件配置为默认选择 null?
- vue.js - Vue js中如何全局分配axios?
- php - 无法在codeigniter中的select 2 jquery ajax中加载数据
- android - 如何从 kotlin 中的 json 获得“错误”或“成功”?
- android - retrofit2.adapter.rxjava2.httpexception http 403 禁止为什么这个错误发生在 kotlin android
- angular - 错误类型错误:t.rgb 不是 angular prod 环境中 C3JS 的函数问题