Firebase with NodeJs | REST API

Akarsh Barar
3 min readJun 3, 2020

In this blog we will see how we can create REST API in Nodejs and connect our app with Firebase.

If you want video tutorial its available on my youtube cahnnel link is in description.

This blog will short and crisp so without wasting any time lets dive right into our code.

Set up node project by npm.

npm init

Now go to firebase console and select the project that you want to connect. Go to the Project Settings then go to the Service account of project and download the private key it will generate a JSON file for you.

Make sure to keep you JSON file private as it contains the private information related to your project.

Now install some dependencies by following command.

npm i express express-generator body-parser firebase method-override morgan

Create files like index.html, server.js.

Rename the file that you have downloaded from firebase as secretKey.json(or any other thing that you like). So now your file structure will be like this

Project Name                       // Project directory
- node_modules // Npm Package
- package-lock.json. // package-lock.json
- package.json // package.json
- server.js // server.js file
- index.html // Html file
- secretKey.json // firebase json file

Now in server.js our main code to connect with it come.

Import the dependencies

var express = require(‘express’);

var app = express();

var Firebase = require(‘firebase’);

var morgan = require(‘morgan’);

var bodyParser = require(‘body-parser’);

Some Basic Configurations

app.use(express.static(__dirname + ‘/public’)); // set the static files location /public/img will be /img for users

app.use(‘/public/uploads’,express.static(__dirname + ‘/public/uploads’));

app.use(morgan(‘dev’)); // log every request to the console

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

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

app.use(bodyParser.json({ type: ‘application/vnd.api+json’ })); // parse application/vnd.api+json as json

Cross Origin Request

app.use(function(req, res, next) { //allow cross origin requests

res.setHeader(“Access-Control-Allow-Origin”, “*”);

res.header(“Access-Control-Allow-Methods”, “POST, PUT, OPTIONS, DELETE, GET”);

res.header(“Access-Control-Max-Age”, “3600”);

res.header(“Access-Control-Allow-Headers”, “Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With”);

next();

});

Setting Up Server

var port = process.env.PORT || 8080;

var router = express.Router();

app.use(‘/v1/api’, router);

app.listen(port);

console.log(‘Magic happens on port ‘ + port);

Setting Up Firebase in Nodejs

Firebase.initializeApp({

databaseURL: “<YOUR DB URL>",

serviceAccount: ‘./secretKey.json’, //this is file that I downloaded from Firebase Console

});

How to add data in Firebase

var db = Firebase.database();

var usersRef = db.ref(“User”);

var data = req.body;

usersRef.push(data, function(err) {

if (err) {

res.send(err)

} else {

res.json({message: “Success: User Save.”, result: true});

}

});

By any REST POST call you can add data to Firebase.

How to fetch data from Firebase

By any REST GET call you can fetch data.

Hope you like this Blog .

Full Video tutorial is on My youtube channel.

Make sure to subscribe to my YouTube Channel :

https://www.youtube.com/c/CodeCave

And follow on Instagram:

@mycodecave

Also on Facebook :

https://www.facebook.com/CodeCave-299370007293562

You can join my whatsapp flutter group the link is in the bio of instagram.

--

--