Node JS Architecture

Malav Live
3 min readMay 4, 2020

--

The architecture of the application is one of the important things which should be kept in consideration when designing the backend application.

To define the architecture we need to first understand what the process or operation we are going to be performed and based on that we can finalize the number of modules we will require who is responsible to perform different operations.

For example, Let’s assume a user from a mobile app requests to join an event organized by other users in the application.

So here is the number of processes or steps which are going to take place.

  • First, we need the server which should be listening to requests sent by the mobile app at a specific port.
  • Connection to the database to store and retrieve the data
  • Validate API Url endpoint
  • Validate the user and identify their permissions for operations
  • Validate Parameters of request
  • Process the request and generate a response

Based on the above operation we can design our structure like the below image.

Folder Structure of Node JS App

Config
-
This script holds the key to retrieve the values from the server’s custom runtime environment variables.
- We can store any private information which we do not want to hardcode in the app.
- For instance, we can store a key to retrieve the private key, which can be used to decode the JWT Token.

Middleware
- Middleware scripts contain a logic to validate a request before proceeding further with the processing of it.
- For instance, We can use middleware which will validate the JWT access token before moving towards the processing of the request. If the request is invalid then it will return with Error else it will be processed.

Auth Middleware that validates JWT Token before passing it to the next module.

As we can see here is one middleware script that will validate the JWT Token and if it’s valid then only it will pass it to the next module to process the request further.

Models
- Model scripts contain the schema of the collection which is like defining the tables with column and it’s types in SQL Database.
- Check out this link for more details about the supported types.
- This also contains the logic of validation of data before inserting it into the database.
- Here is a sample model file containing different properties of the user.

User Model

Routes
- Route scripts contain the list of valid endpoints and business logic for specific requests.
- It’s responsible for communicating with the database and provide a response to the request.
- Make sure to follow REST API Protocols while making the APIs.

Startup
- This folder contains different scripts to load different modules and execute one-time operations.
- For instance, We can have a database script that will create a connection between the application and MongoDB database.
- Or we can have a script that assigns appropriate modules to different URL endpoints.

Finally Index.js

  • it’s the starting point of node application where it calls different modules that will be required to process the requests.
  • For example, We can call the routes scripts to handle supported API Url and database script to create a connection between the Node app and MongoDB Database.
index.js

To run the application

  • Open the terminal
  • Move to the folder containing index.js file.
  • and run the command node index.js

I hope this article explains the roles and responsibility of different modules and how each one of the modules communicates with each other and produce a response for each and every request received.

Check out a complete project containing basic CRUD operation for the user’s collection.

GitHub: https://github.com/malavsoni/API-Starter-Pack

Thanks for reading this article.

Enjoy the power of technology….!!!!

Happy Coding…!!!

Next articles coming soon…

--

--