RESTful Web Service with Node.js, Google App Engine and Firebase Real-time Database


RESTful web services are based on REST (representational state transfer) architecture. A RESTful web service exposes a set of resources through URIs. Resources are manipulated using a fixed set of four operations, create (PUT), read (GET), update(POST) and delete(DELETE). Usually every interaction with a resource is stateless such that request messages are self-contained.

For more details about RESTful web service please go through this article.


In this article, we are going to develop a simple RESTful API as per the steps given below. We will use Google App Engine to create and deploy RESTful web service.

  1. Setup Google Cloud Account.

You can log in to Google cloud console using your Gmail account. Google cloud platform can be used for free trials and each account is given a grant of 300 dollars to explore Google cloud products. How ever credit card details are required by Google in case the usage exceeds the free quota. But free credit is more than enough to explore cloud products.

2. Create New Project.

Once you have successfully created a google cloud platform (GCP) account, you can access the console. You can create new project using “Select a Project” drop down in top left corner (Image:1).

Image:1 Google Cloud Console

3. Create app engine instance

Once you created a new project, select it from “Select a project” drop down. In left side menu, under compute there is an item called app engine (Image:1). Click it and create new app engine instance. As we are using Node.js for this tutorial, select it as the language of the app (Image:2). Then select appropriate region to host the server. After that Google cloud platform will create and deploy the project.

Image:2 App Engine Setup

4. Activate Google Cloud Shell

Once the project has successfully deployed by GCP, Google Cloud Shell can be activated by using the tool bar in the top of the window (Image:3).

Image: 3 Google Cloud Shell

We can use below commands to create source directory to host our project.

mkdir <SourceFolderName> // create source directory
cd <SourceFolderName> // navigate to source directory

Once you created and navigate to the source folder, code editor can be activated by using the pencil icon given in right side of the cloud shell tool bar (Image:3).

5. Create Server

Initially we need two files (server.js and package.json) to start the project. Package.json file is used to list dependencies for the project so create it and copy below content to that file. Initially it will be an empty dependencies list.

These files can be created as given in Image:4.

Image:4 Code Editor

We will use Express Framework to create the server. How ever we need to install it. We can use npm package manger to install required packages using below commands,

npm install express --save

After that package.json file should be updated as follows,

Add below content to server.js file,

You can deploy server by executing “node server.js” command from the source folder. Server will get started and display message “server listening..” in the console.

We can check whether server is working properly by using the web preview option given in right top corner of cloud shell window.

Image: 5 Web Preview

Once you click on the “Preview on port 8080”, new tab will be opened in the browser and server reply (in this example: “Get Request”) will get displayed in the tab. You can use URL of that tab to test RESTful web service later.

6. Add other REST methods

As mentioned at the beginning, RESTful web service uses four methods (put, post, get and delete) to manipulate resources. We have already added a method to handle get request. Like that we can add methods to handle other request types as given below.

You can test these services using a REST client by using the URL obtained from web preview. Example is given in image 6.

Image: 6 Restlet Client

You can implement above functions as per your requirements.

7. Initialize Google Firebase real-time database

Google Firebase is a cloud hosted NoSQL database. Before using it, our project should be registered in Google Firebase through the console as given in image 7.

Image: 7 Google Firebase Console

Once we added our project successfully, we can select real time data base using the left side menu of the console as given in image 8. We can start database in test mode for this tutorial and later permissions can be modified appropriately.

Image 8 : Realtime Database

Once we start the project, browser will be navigated to database view. It has subsections to view data and permission as given in image 9.

Image 9: Database Details

8. Integrate Firebase server to web service

Required packages for Firebase database can be installed using below commands. These commands should be executed from your source location.

npm install firebase --save
npm install firebase-admin --save

After that, packages.json should be updated as follows,

After that following code should be added to server.js to initialize database in our application.

Required configuration details can be obtained from project overview as given in image 10. We can simply copy and paste the content within the config struct to our code as given in image 11.

Image: 10 Project Overview
Image: 11 Configuration Details

To test the whether integration is working properly, we can modify HTTP get method in our server as given below,

Once we rerun the server and send HTTP GET request from browser/REST client, database should be displayed as given in image12.

Image 13: Database View

9. Create, edit, read and delete data in Firebase database

After writing simple value to the Firebase database, we can extend the functionality to read values from request body and write to the database. We need to install package to parse request body using below command,

npm install body-parser --save

Below sample web service can read username, name and age of user from request and insert it to database (PUT/POST). Also it can retrieve list of users from database and send as response (GET).

These services can be tested using Restlet client as given below.

Image 14: GET/PUT Requests