简体   繁体   English

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

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

I am trying to set up the firebase emulators for functions and firestore.我正在尝试为功能和 Firestore 设置 firebase 仿真器。 My file structure is set up like this:我的文件结构是这样设置的:

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

The view is where my react frontend code is.该视图是我的反应前端代码所在的位置。

I have the emulators set up and when i run firebase emulators:start it runs the emulators.我设置了模拟器,当我运行firebase emulators:start它运行模拟器。 But after that, i don't know how to connect them with nodejs and the frontend.但在那之后,我不知道如何将它们与 nodejs 和前端连接起来。

this is the index.js code for the path functions/index.js:这是路径函数/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);

In functions/util/admin.js, I have code as such:在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 };

this is some of my code for functions/APIs/users.js to log in and such:这是我用于登录的函数/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 ****/}

If anyone has any advice as to how to connect the emulators, please let me know.如果有人对如何连接模拟器有任何建议,请告诉我。 Also, if anyone has better suggestions or needs more info please tell me.另外,如果有人有更好的建议或需要更多信息,请告诉我。

Thank you for any help!感谢您的任何帮助!

Not enough imaginary internet points to comment, so I'm putting my thoughts here.没有足够的想象互联网点来评论,所以我把我的想法放在这里。 BTW I don't have a background in Ionic so I could be missing something.顺便说一句,我没有 Ionic 的背景,所以我可能会遗漏一些东西。

The emulator suite, when active, should be available at a localhost address (eg localhost:5001 which I believe is the default).模拟器套件在激活时应该可以在localhost地址上使用(例如localhost:5001 ,我认为这是默认设置)。 From there you should be able to route to your functions like so localhost:5001/example_function .从那里你应该能够像这样路由到你的函数localhost:5001/example_function

This should only be used for developing things locally.这应该只用于在本地开发东西。 When deploying the app live, it should be deployed firebase deploy and the url will change based on your firebase project.实时部署应用程序时,应部署firebase deploy ,url 将根据您的 firebase 项目而更改。 In both cases the CLI should write the exact function urls在这两种情况下,CLI 都应该编写准确的 function url

Let me know if that answered you question or if I was way off!让我知道这是否回答了您的问题,或者我是否离题了!

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

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

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