【问题标题】:Express app on Firebase Functions locally本地 Firebase 函数上的 Express 应用
【发布时间】:2021-03-17 15:48:56
【问题描述】:

我使用 Firebase Functions 创建了一个 Express 应用,并将文件托管在 Firebase Hosting 上。

index.js(Firebase 函数):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const parse = require('body-parser');
const cors = require('cors');

admin.initializeApp();

const app = express();

app.use(cors({ origin: true }));
app.use(parse.json());
app.use(parse.urlencoded({ extended: false }));

app.post('/create-url', (req, res) => {
    console.log(req.body);
    res.send(req.body);
});

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

firebase.json:

{
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "function": "app"
      }
    ]
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "hosting": {
      "port": 5000
    },
    "ui": {
      "enabled": true,
      "port": 4000
    }
  }
}

当我向 http://localhost:5000/create-url 发出 HTTP 请求时,req.body 是 emtpy,但是当我向 http://localhost:5001/PROJECT_NAME_HERE/us-central1/app/create-url 发出请求时(我已经隐藏了项目名称),它可以正常工作。我正在使用 Postman 发出请求,但它也不适用于我的前端代码:(。如果我部署到 Hosting and Functions 并通过 web.app 域访问应用程序,它就可以工作。我在running firebase emulators:start 在本地运行。

提前感谢您的帮助!

【问题讨论】:

    标签: node.js firebase express google-cloud-functions firebase-hosting


    【解决方案1】:

    查看本页底部https://firebase.google.com/docs/emulator-suite/install_and_configure。 '生成身份验证令牌(仅限主机模拟器)'。

    基本上,您必须生成一个身份验证令牌并将其作为标志传递到您的构建脚本中,或者将其添加到您的 ci 环境变量中。

    如果有帮助请告诉我!

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多