Node Js Project Folder Structure

Node Js Project Folder Structure
Table of Contents

Node js is a runtime environment which enables javascript to run outside the browser. If you don’t have node js you can download it from here – Download Node Js. Node js has become popular backend framework and it is used by many developers for backend development because it is very fast and secure.

Today I am going to tell you the node js project folder structure to use in your node application. This node js project folder structure is very organised and easy to manage I use this node js project folder structure in my every project so lets start.

Note that this node js project folder structure is only used for seperate backend application there has to be seperate frontend folder or app with react, angular or html.

Node Js Project Folder Structure

Node js project folder structure

It can be a pain to work on a un-managed node js project and you will not feel working on it so it is important to have a project folder structure. You see all the folders which are required for a node js application each folder holds great power. I am going to explain you what each folder does. 

If you want to get folder icons for vs code than you can install these extension Material Icon Theme

If you want to become a backend developer than read this article – Roadmap to become a backend developer.

server.js – server js is the main file of your backend application this is where node looks when you start your application. It has all the routes of your application, In this file you start your server, connect to your database and do server configuaration. You can name this file anything like app.js, index.js I like to name it server.js because it’s relatable.

routes – This folder has all your routes which you are going to create. You connect your routes to your controllers here.  You can also add middlewares to your routes here. They are like your api routes. You need to import this routes in your server.js file inorder to use.

controllers – We don’t write the logic of route in the routes file we seperate them in seperate folder called controllers. We store each route controller in seperate file this makes your code look organised and managed. you need to import these controllers in your routes folder.  

middlewares – These folder holds all the middlewares of your backend application like the authentication, authorization or verification middlewares. To apply the middlewares you need to import it into your routes folder.

models – This folder contains your database schema and models. You have to design the schema of the model and export  it and then you can use it to read and write to the database. You have to import it in controllers or middlewares.

package.json – Then we have package.json file generated by node js. this file has all the node js project dependencies if you ever delete your node modules than you can use npm install to install them back node will look into package.json for required modules.

node_modules – This is the place where all your installed libraries files go. You never should edit or change any file in the node_modules folder.

.env – This file stores all your secret keys. You should not push this on github because it contains secret keys of your application.

Summary

So this was the node js project folder structure this was very short article I hope you found what you were looking for if you loved this do subscribe to our newsletter and comment down if you loved this node js project folder structure thank you. 

If you want a react frontend folder structure than comment down I want react. thank you for reading Have a nice day 🙂

If you want to become a full stack web developer than read this article – Roadmap to become a full stack web developer.

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles