Organise NodeJs Application

Organizing the file structure of the application is one of the major tasks in development. If the application is well structured, it is easy to read and maintain. let's explore the folder structure and the files of the folder contained within them

Folder structure


Config

Config folder contains the configuration of your server and the database connections. It contains methods to connect with the DB and set up the database. e.g. 

    
try{
const conn = await mongoose.connect(mongoDb.Database.host,{
useNewUrlParsers: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
console.log("connection to db established",conn.connection.host);
}

This try method will try to connect to a MongoDB instance along with the defined parameters.

Constants

As the name suggests, this folder contains all the constants of the node application, e.g. Database name, hosting address, and other global consts.

Controllers

    
The controller contains all the controller files of the project. It contains the functions which will perform all the CRUD (Create, Read, Update, Delete) operations for the API. This file also contains authentication controllers which will perform the sign-up and sign-in functionalities.

Model

The model contains all the models for the database. It contains the schema for the database.

Utils


This folder contains extra services and utility classes for the APi.for example this file could contain an Error.js file which will perform the functionality of the specific error based on the type of error.

Routes

This folder will contain all the routes of the application which will be accessed in the API. e.g. it could contain an authentication router that will call the authentication controller whenever the authentication router is accessed.

Conclusion

This is the basic structure that a node application needs to have in it. The application structure allows the management of the code easily when the app starts to scale. so arranging the node app according to folder structure must be a practice that every developer needs to follow


Post a Comment

 
Top