简体   繁体   中英

How to set up firebase emulators using nodejs and reactjs?

I am trying to set up the firebase emulators for functions and firestore. 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. But after that, i don't know how to connect them with nodejs and the frontend.

this is the index.js code for the path functions/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:

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:

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.

The emulator suite, when active, should be available at a localhost address (eg localhost:5001 which I believe is the default). From there you should be able to route to your functions like so 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. In both cases the CLI should write the exact function urls

Let me know if that answered you question or if I was way off!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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