Blog Banner Image

How to Build a Clean Node.js Application with Express Framework?

Node.js is a full-stack MVC framework that has grown in popularity over the last few years. Today it has become a universal development framework bundled with scaffolding, template engines, WebSocket, and persistence libraries to allow organizations build light-weight, efficient, and data-intensive real-time scalable web apps. According to Stackoverflow survey, Node.js and AngularJS continue to be the most commonly used technologies.

Node JS development

Express.js is a Node.js web application framework, which can be used for building single-page, multi-page, and hybrid web applications. The Node.js web app development with Express framework requires developers to keep the following things in mind:

  • Environment where the server is run
  • Generic responses
  • Generic routing
  • Logging
  • Clean code management

How to Generate Node.js App

Installation

The Express framework can be installed with the help of a Node Package manager. This can be done by executing the following code on the command line.

npm install express-app-generator

This code requests the Node Package manager to download the required Express modules and install them. Once the framework is installed, we can generate the Node.js app using the following code.

Node JS development 2

Note: If you want to change Express properties, you can change it by using the commands—app.use() or app.set()

The Node.js app generated has some default express features:

Node JS development 3

Basic Routing

Routing refers to how an application responds to the client request to a particular endpoint. The AppRouter method of the app generator helps make routing flexible. The developers also need to do the following things for making the routing process clean and easy-to-implement:

  • The api folder should be in the root of the app. This folder should contain all the .js files with s or es suffix. For example, users.js, user.js, employees.js etc.
  • The logs folder is a must as it contains all the logs as json files, which the developers get in console.

The basic router provides a simple CRUD mechanism which builds a combination of arrays and objects. In order to get started for routing using the REST or the CRUD mechanism, we need to use the below code.

let api = app.appRouter;  //app is an instance of the Express generated app

api.model (‘users’).register (‘REST’, [ authentication]);

After the code is executed, the URL generated will be http://localhost:3789/api/users.

The array which is present just after the REST keyword is used for middleware. Suppose, if you want authentication to happen, the function should be passed in the middleware array.

We can use REST or CRUD keyword to create, update, delete, get, and search methods that are ready to be used. However, to make this happen you must have all the methods in the /api/users.js file.

  • If the request is for the POST method, then it should go in expots.create=(req,res)=>{};
  • If the request is for the GET method, then it should go in expots.get=(req, res)=>{};

         The following URL will be generated http://localhost:3789/api/users/:id

  • If the request is for the GET method, then it should go in  expots.search=(req, res)=>{};

The following URL will be generated http://localhost:3789/api/users. In this URL, you can also pass various query parameters using ?.

  • If the request is for the PUT method, then it should go in expots.update=(req,res)=>{};
  • If the request is for DELETE method, then it should go for expots.delete=(req,res)=>{};

Note: All the middleware functions must have parameters (req, res, next) where req and res are request-response Express generated objects and next is the callback function. next object is called when one middleware work is done. After one function is over, it goes to the next middleware function (if present) in that array to perform the function. When all the middleware functions have been executed, next object goes to the main function in the users.js.

Handling User-defined Requests

In order to handle user-defined requests, developers must ensure that:

  • In the register method, action and method are present
  • There is a filter for the middleware and filters for multiple requests

Code to Handle Single Requests

Node JS development 4

Code to handle Multiple Requests

Node JS development 5

Note: ‘api’ keyword in the URL is constant all over the router. So, your base URL will be http://localhost:3789/api/.

Responses

There are four types of responses for all the responses if the status code is 200.

1. Data Request

This request takes one or two parameters—data, message—out of which data is mandatory.

The below code will generate the response to the above request:

2. Page

  • This type of request takes one or two parameters—items, pageSize, pageNo, totalRecordsCountitems—which are mandatory
  • This request can also be used for paginations
Node JS development 6