Build Express App with Firestore and Deploy on Choreo

A Step-by-Step Guide to Integrating a Database and Deploying to the Choreo Platform

Ishan Hansaka Silva
7 min readJun 30, 2024
photo by wso2/choreo

In this blog post, we will walk through the process of creating a server that integrates with a simple database to store your project details, and then deploy it to the Choreo platform. We will use Express.js for the server and Cloud Firestore for the database.

Photo by ikramkharbouch on dev.to

Overview of Technologies

Express.js, a minimal and flexible Node.js framework, streamlines the creation of server-side applications with its robust set of features and middleware support. Coupled with Cloud Firestore, a NoSQL document database from Firebase. Choreo offering seamless API management, event streaming, and microservices integration. This intuitive platform, developed by WSO2, a leading software company in Sri Lanka, simplifies the deployment and management of your applications, making it easier than ever to build and scale your projects.

Setting Up the Project

Before we dive into the code, make sure you have the following installed:

In this tutorial, we will cover the following main steps:

  1. Set Up Express.js
  2. Initialize Firebase Admin SDK
  3. Add Routes for Firebase GET and POST Operations
  4. Deploy on Choreo

Step 01: Set Up Express.js

  • Initialize Your Project

First, create a new directory for your project. Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:

npm init -y
  • Install Express

To install Express, use npm. This command installs Express to your project.

npm install express
  • Create Express App

Create a new file for your Express application, for example, app.js or index.js in the root directory of your project. Require Express and create an instance of the app:

Step 02: Initialize Firebase Admin SDK

  • Set Up Firebase Project:

Go to the Firebase Console, create a new project, and add a web app. For additional knowledge and detailed setup instructions, please visit the official Firebase documentation on setting up Firebase for web apps: Firebase Web Setup Guide.

  • Generate Service Account Key:

go to project settings in the Firebase Console, navigate to “Service accounts” and click “Generate new private key”. This action creates a JSON key file essential for securely authenticating your application with Firebase services. Download and securely store this file in your project directory.

photo by Author
  • Setup Firebase Admin SDK in Express app:

First, install the Firebase Admin SDK npm package using npm, this command installs the Firebase Admin SDK package, enabling server-side access to Firebase services.

npm install --save firebase-admin  

Then, load the service account credentials from the JSON file and initialize Firebase Admin SDK.

Once initialized, you can use Firebase Admin SDK methods to interact with Firestore. You want to create cloud firestore database in firebase and add databaseURL from your project_id.

Step 03: Add Routes for Firebase GET and POST Operations

In this step, we’ll add routes to our Express application for performing GET and POST operations with Firestore.

GET Endpoint (“/projects”)

POST Endpoint (“/projects”)

Before we proceed to set up the POST method, it’s essential to ensure our Express application can handle JSON request bodies effectively. To achieve this, we’ll use body-parser, a middleware for parsing JSON and URL encoded request bodies. First, let’s install body-parser using npm:

npm install --save body-parser

Then, these functionalities can be implemented within the index.js file of your project.

Now, let’s dive into setting up the POST method to add project to Firestore.

You can use tools like Postman or Insomnia to send HTTP requests to your server and verify its functionality. In this example, I send HTTP requests through Insomnia to interact with your server endpoints.

photo by Author

Cloud Firestore database is shown below.

Having completed the third step of adding routes for Firebase GET and POST operations, we will now proceed to the final step, deployment on Choreo.

Step 04: Deploy on Choreo

Once you have set up your GitHub account and created a repository containing your service implementation, follow these instructions to deploy your application using Choreo.

This process will streamline the deployment of your service onto Choreo’s platform.

1. Create a project

photo by Author

2. Create a service component

On the project home page, click Service under create a component.

photo by Author

· Go to GitHub tab and add your repository URL

To allow Choreo to connect to your GitHub account, click Authorize with GitHub. If you have not already connected your GitHub repository to Choreo, enter your GitHub credentials and select the repository you created in the prerequisites section to install the Choreo GitHub App.

photo by Author

· Select the NodeJS buildpack

· Set NodeJS project directory

As part of the deployment process, you need to create an endpoints.yaml file. This file is used to specify the details of the external services your Express app needs to connect to, such as databases or APIs. learn more.

File directry => .choreo/endpoints.yaml

· Create openapi.yaml file in your project

Creating an openapi.yaml file is crucial for standardized API documentation, automated testing, validation, and client library generation. It ensures consistent API design, integrates with various tools, and aids in deployment automation, simplifying API management and usability.

To maintain consistency in your openapi.yaml file, use online tools like Swagger Editor which provides real-time feedback and highlights errors. When executing endpoints in Swagger Editor, you might encounter "Failed to fetch" errors due to CORS issues. Resolve this by installing the cors package in your project and updating your Express.js app to use the cors middleware, ensuring smooth execution of your endpoints.

Once this is completed, you want to pull origin your changes to repository and we can proceed to the next step.

· Click Create

You have successfully created a Service component with the NodeJS buildpack. Now let’s build and deploy the service.

photo by Author

· Build and deploy

Build

To build the service, follow these steps:

1. On the project home page, click the Book List component listed under Component Listing. This takes you to the component overview page.

2. In the left navigation, click Build.

3. Click Build Latest.

Once the build process is complete, the build status changes to Success.

photo by Author

Deploy

Now you are ready to deploy the service. Follow these steps:

1. In the left navigation menu, click Deploy.

2. On the Set Up card, click Configure & Deploy.

3. In the Environment Configurations pane, click Next.

4. In the File Mount pane, click Next.

5. Review the Endpoint Details and click Deploy.

photo by Author

Summary

This blog has walked you through setting up an Express.js server integrated with Cloud Firestore for managing projects. You’ve learned to create REST API endpoints and deploy your application on Choreo, enabling scalability and efficient cloud-based operations. These skills prepare you to build resilient server-side applications effectively leveraging cloud database solutions.

References

--

--

Ishan Hansaka Silva

Undergraduate at University of Moratuwa Faculty of Information Technology Technical Writer at LinkIT Publication https://www.linkedin.com/in/ishanhansakasilva/