Instagram
youtube
Facebook
Twitter

How to Connect Firebase with Express

How to Connect Firebase with Express?

  • Firebase is a set of hosting services for any type of application.
  • It offers NoSQL and real-time hosting of databases, content, social authentication, notifications, or services, such as a real-time communication server.

Let's Begin with setting up a firebase

  • Click on the following link: firebase console.
  • Click on Add project. Enter the project name and click on Continue.

  • Click again on continue and then choose a google analytics account and click on Create project.

  • Click on Ok after completion of the project creation. Click on add app.

  • Click on the web button and then give a name to the app and click on register.

 

  • After registering, copy the following code and paste it to notepad or word for further use.

  • Click on continue console. Now scroll down and select cloud Firestore.
  • Now click on create database and pop up appears on the screen select "start in test mode".

  • Click on next and then click on enable.

  • After the Database is created.

  • Now let's move to the code part, open vs code, and run the command on the terminal.
npm init -y
  • Install the packages using the following command in the terminal.
npm install express, firebase, cors

  • Here is an application file structure.

  • For connecting the server with Firebase do the following code in config.js. Paste the following code copied in the previous step in the config.js.
const firebase = require("firebase");
const  firebaseConfig = {
  apiKey: "AIzaSyD2S2hIoiu_8XyMzJ3WbPVRKIByx8sYAGQ",
  authDomain: "project1-1dcf0.firebaseapp.com",
  projectId: "project1-1dcf0",
  storageBucket: "project1-1dcf0.appspot.com",
  messagingSenderId: "929831393635",
  appId: "1:929831393635:web:06cd5c6a7c14977d109b56",
  measurementId: "G-TWMQKW3SCF"
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const User = db.collection("Users");
module.exports = User;
  • All the crud operation is coded in the following files.

In index.js

const express = require("express");
const cors = require("cors");
const User = require("./config");
const app = express();
app.use(express.json());
app.use(cors());

app.get("/", async (req, res) => {
  const snapshot = await User.get();
  const list = snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
  res.send(list);
});

app.post("/create", async (req, res) => {
  const data = req.body;
  await User.add({ data });
  res.send({ msg: "User Added" });
});

app.post("/update", async (req, res) => {
  const id = req.body.id;
  delete req.body.id;
  const data = req.body;
  await User.doc(id).update(data);
  res.send({ msg: "Updated" });
});

app.post("/delete", async (req, res) => {
  const id = req.body.id;
  await User.doc(id).delete();
  res.send({ msg: "Deleted" });
});
app.listen(4000, () => console.log("Up & RUnning *4000"));

  • Open the postman and check the APIs.
  • GET

  • POST 

 

  • After all the operations are done, Once observe the change in the firebase database too.