繁体   English   中英

如何使用 nodejs 和 reactjs 设置 firebase 仿真器?

[英]How to set up firebase emulators using nodejs and reactjs?

我正在尝试为功能和 Firestore 设置 firebase 仿真器。 我的文件结构是这样设置的:

functions
---APIs
---util
---.gitignore
---index.js
---package-lock.json
---package.json
views
---public
---src
---.gitignore
---capacitor.config.json
---ionic.config.json
---package-lock.json
---package.json
---tsconfig.json
.firebaserc
.gitignore
firebase.json
firestore.indexes.json
firestore.rules
ionic.config.json
package-lock.json
package.json

该视图是我的反应前端代码所在的位置。

我设置了模拟器,当我运行firebase emulators:start它运行模拟器。 但在那之后,我不知道如何将它们与 nodejs 和前端连接起来。

这是路径函数/index.js 的 index.js 代码:

const functions = require("firebase-functions");
var bodyParser = require("body-parser");
const app = require("express")();

const auth = require("./util/auth");

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

{/**** route calls were here ****/}

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

在functions/util/admin.js中,我有这样的代码:

const admin = require("firebase-admin");

admin.initializeApp();

const db = admin.firestore();

db.settings({
  host: "localhost:8080",
  ssl: false
});

module.exports = { admin, db };

这是我用于登录的函数/APIs/users.js 的一些代码等:

const { admin, db } = require("../util/admin");
const config = require("../util/config");
const { capitalize_Words } = require("../util/helpers");

const firebase = require("firebase");

firebase.initializeApp(config);

const { validateLoginData, validateSignUpData } = require("../util/validators");

// Login
exports.loginUser = (request, response) => {

{/**** rest of code here ****/}

如果有人对如何连接模拟器有任何建议,请告诉我。 另外,如果有人有更好的建议或需要更多信息,请告诉我。

感谢您的任何帮助!

没有足够的想象互联网点来评论,所以我把我的想法放在这里。 顺便说一句,我没有 Ionic 的背景,所以我可能会遗漏一些东西。

模拟器套件在激活时应该可以在localhost地址上使用(例如localhost:5001 ,我认为这是默认设置)。 从那里你应该能够像这样路由到你的函数localhost:5001/example_function

这应该只用于在本地开发东西。 实时部署应用程序时,应部署firebase deploy ,url 将根据您的 firebase 项目而更改。 在这两种情况下,CLI 都应该编写准确的 function url

让我知道这是否回答了您的问题,或者我是否离题了!

https://firebase.google.com/docs/emulator-suite/install_and_configure

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM