Maximizing the Power of REST API with Amplication for NodeJS

In today’s internet era, we are always only a few clicks away from the information we need, thanks to social media, search engines, eCommerce websites, etc. A good example would be your favorite site for booking flights. All you have to do is put the dates and locations for your trips, and you get flights in a split second. Behind the scenes, this data came from an API, a crucial component of what makes the internet so useful.

In this article, we’ll learn what a REST API is. We’ll start by introducing its concepts and their importance. Then, we’ll discuss some common ways to optimize your REST APIs performance within the context of NodeJS. Finally, we can’t ignore security, so we’ll tackle a bit of that. 

Then you’ll create a NodeJS REST API using this step-by-step guide with the help of Amplication. Data modeling and security are part of creating a whole application from scratch. 

OK, let’s get started then.

REST APIs and Their Importance in Modern Web Development

In this section, we’ll discuss what an API is, then introduce a metaphor for us to visualize it. Then, using this metaphor, we’ll discuss the benefits of APIs in today’s modern development. 

What is an API? 

It is important to understand APIs first before going further. API stands for – application programming interface. It is an interface for client-side applications like websites, mobile, and desktop to communicate with the backend logic. 

To make it simpler, it is like a messenger that takes the request from the users and sends the request to the backend system. After that, the backend system will respond to it by passing data to users. A good metaphor is a waiter/waitress who can understand the needs of their various customers’ orders. They serve as a go-between for the restaurant’s clients and its chefs in the kitchen.

Benefits of APIs

Continuing our waiter/waitress metaphor, imagine if you were the restaurant’s owner. The wait staff would act as the go-between between you and your customers. There would be many benefits of having a wait staff. 

If your clients don’t see your kitchen, you can protect your trade secrets (your recipes). After all, they don’t have to be aware of the cooking process. They only have to place an order to get the dish.

Now, let’s move to the computer science world. The nearest technical term for this is “encapsulation”. Encapsulation is an important advantage of having an API. It prevents outsiders from seeing the business logic. For instance, big corporations with sensitive information might have to make the information selectively accessible to outsiders. So, they need to feel confident about their system to expose their internal data to the public through an API.

What is a REST API?

REST (representational state transfer) also known as RESTful API is a type of API that conforms to the REST constraints and principles. Remember, REST isn’t a protocol; it is more of a software architecture design, and many software developers follow this design to build their applications. 

Rest Api
Source

For example, social media sites like Facebook, Twitter, LinkedIn, and Tiktok use APIs to build their applications. These companies show that REST is a scalable design.

Tips for Optimizing the Performance and Scalability of Your REST APIs

Within the context of NodeJS, we’ll discuss those crucial areas we need to be aware of when it comes to optimization and scalability. 

Your API needs scalability for handling large amounts of traffic, and it needs to be optimized for the purpose of speed. These two come hand in hand. That’s why it is important to improve the response time of your API: the shorter the response, the faster it will be for your consumers. 

There are many things you can do to improve performance and scalability. Today, we will tackle the following three: 

Optimize Database Queries

Regardless of your database, query optimization is integral to building a large API in NodeJS. For example, let’s say you have created a database without performance in mind. Just imagine what would happen in the future if you encounter a situation where a lot of users are accessing the same data. This may hurt your API. 

That’s why it is recommended that you at least be aware of the following: 

  • Select query – Avoid selecting all columns to avoid extra load on the database.
  • Indexing – Ensure the proper indexing that can result in fast access to the database.
  • Subqueries – Avoid correlated sub-queries as it finds data row by row that impacts the speed of SQL query processing.

Cache Your Application 

In any application, caching is vital because it helps the performance of your API. There are a lot of third-party libraries and frameworks that can help you use cache mechanisms in your application. 

A popular one is Redis, which is an advanced version of Memcached. It is a solution for distributed memory caching. Just reducing the number of times an external data source, like a database, must be read and then caching it can speed up your API significantly.

Use Asynchronous Functions

It is vital to use async functions in your NodeJS projects always. You may not agree, but most JavaScript folks accept that async functions are the heart and soul of JavaScript. Writing asynchronous functions to carry out nonblocking I/O operations is the best way to optimize CPU utilization. This process of reading and writing data is categorized as I/O operations. The local storage disk on which the I/O operations are carried out can be the database, cloud storage, or any other type.

Recommendations for Maintaining the Security and Reliability of REST APIs

There are many ways to secure NodeJS API, but at the bare minimum, we need to have authentication and authorization at least. Moreover, in today’s web development industry, developers use JWT tokens for their authentication and authorization. 

Let’s see them one by one. 

Authentication and Authorization 

The process of determining whether or not someone or anything is who or what they say they are is known as authentication. It can regulate access by checking if a credential matches those in a database of authorized users or a data authentication server.

Authorization is how a system determines if a user is allowed to access or use a particular resource. Authentication and authorization are frequently used together so that the server can identify the user who is accessing a certain resource.

JWT Token

JWTs (JSON web tokens) are transmitted between the user and the server during user authentication. They function by encoding the user identity and digitally signing it to create an unforgeable token that serves as the user’s identification. Then, the software can utilize the user’s identity to regulate access. Including the header, payload, and signature together creates a JWT string. 

Using Amplication to Improve the Performance, Scalability, and Security of REST APIs in NodeJS

Now that we have a solid understanding of what a RESTful API is, as well as advice on how to enhance, scale, and secure your NodeJS API, let’s understand how we can simplify the process. 

Let’s say that due to a pressing deadline, you must create a prototype right away without giving optimization, scalability, and security for your NodeJS API much thought; or you need to deliver a proof of concept right away; or you are tired of working on your application’s foundational components, such as security, optimization, and scalability. 

Amplication, which we will define in the next section, can assist us.

What is Amplication?

It takes time for developers to design their backend. This is because it includes deeper thought to create a good backend. Moreover, segmenting larger problems into smaller parts is necessary when designing the data model. For example, consider the tips we have given till now in the areas of optimization and security when using NodeJS. That requires a lot of critical thinking. 

This is where Amplication comes into the picture. Amplication can help developers create data models and built-in role-based access management and provide code that can be downloaded right away or uploaded to GitHub. How does that sound? Cool, right? Amplication is a flexible open-source platform for Node.js app development.

Setting up Amplication for REST APIs in NodeJS

To get started, you need a GitHub account and you need to authorize Amplication. You’ll see something like this when you start to log in

Authorize Amplication

Once everything is set up, you’ll be able to see your dashboard. 

Amplication Dashboard

Now, that we have the account needed to get started. Here are the steps for autogenerating production-ready backend code.

Steps for Creating A Simple Employee Maintenace Application

Sync With GitHub 

It is recommended that you sync your GitHub account with the project you created on GitHub. This is because once you decide to commit and build, it will automatically push the auto-generated code to the project repository you have chosen. Cool, right? 

Github Sync

Create Roles

Before we create the entity for storing the employee’s information. Let’s just have an admin so we have all privileges later when we create the employee entity. 

Graphical user interface, text, application

Description automatically generated
Github Roles

Create Entities 

We need to create the entity related to our employee, so we can store it within our application. Just go to the “Entities” section and click the button “Go to entities”.

Create Entities

After that, we can now define the fields of our entity. In our example, we just created the following: first name, last name, middle name, birthdate, and date hired. 

Entity Fields

You can add more as you like but for our example, this is enough. And of course, we can’t ignore security. We created the admin role user at first, then we configured how the user and admin will access the entity. How convenient is that? Remember how long it took you to figure it out when you were a beginner with NodeJS? With Amplication, this is auto-generated for you.

Entity Security

Code View and Commit 

Once you have made the roles needed, you can commit and push your code.  Just find the “Code View” section on the dashboard.

View Code

Once you are ready with the commit, just click “Commit changes and build”. 

Commit Changes

Give it a few minutes as it finishes compiling and building the application, and the result will be amazing. You’ll have a new branch on your GitHub repository including the auto-generated code of the entity, and security that you have configured. 

Project View

Download Source Code via GitHub 

That’s pretty much it. Once it generates the source code it will give two sets of the directory.  The admin-UI and server. 

If you click the admin-UI you’ll see the repository codes and instructions for you to run and deploy your application same with the server:

Download Code
Github Project

Conclusion

This article started with the concepts of API and the benefits of optimization, scalability, and security of NodeJS. Of course, it is good to know and apply them to your project. But there are times you need to have an easy backend code generator as part of your tool, as it makes code development faster and lets you focus on the business side of the application. 

Amplication is one such tool that generates the minimum backend API services you need as a developer. It can generate based on your configuration, such as your repository, performant entities, and related securities that secure your application. 

NodeJS is the perfect platform for setting up REST APIs due to its effective performance and scalability. Moreover, when creating APIs and implementing interaction with pre-existing applications, there are a variety of ready-made and useful modules for working with pure HTTP(s), REST API, web services, sockets, etc. However, time constraints and complexity hinder this process. In such instances, data models and securities can be automatically created using tools like Amplication.

Pankaj Kumar
Pankaj Kumar
Articles: 207