DEV Community

Cover image for Stop the Mess: Structuring Your First Node.js Application
Saleh Ahmed Mahin
Saleh Ahmed Mahin

Posted on

Stop the Mess: Structuring Your First Node.js Application

Today, I'm going to talk about a folder structure that is specifically designed for beginners.

When you first start making a Node.js app, you might put all your code into just one file, maybe called index.js or app.js. It's like putting everything in your kitchen - the food, the pots, the cleaning supplies - all into one big box! That works at the beginning, but soon your file gets really long and messy, making it hard to find things, fix mistakes, or add new features later on.
Example:
A folder structure of basic project

What's the issues?

Every beginner starts with the basic files like index.js and package.json. This is perfect for a simple app or when you are just learning. As the project grows larger, keeping all your code in one file becomes a massive headache, making the entire application complex and hard to manage.

Which type of complexity?

On a large project, you write a huge amount of business logic, create many APIs, and manage all the database and schema details. Putting all that code into one place makes it a complex, unreadable mess, which quickly creates major maintenance issues and makes it almost impossible for new team members to jump in.

How to fix this complexity?

To make things easier as your app grows, you should start putting different parts of the code into their own separate files. For example, your api routes go in one folder, your all business logic (controllers) in another and so on. This keeps your code neat and organized, making your life much easier in the long run.

How I Organized My Node.js App: A Step-by-Step Folder Structure Guide

Organized folder structure

I use this specific folder structure for my basic projects, such as a simple e-commerce application.

Let's breakdown it:

  • Authorization:

Authorization

I primarily use the authorization folder to handle all my authentication and authorization-related API logic, such as tasks like creating JWTs (JSON Web Tokens).

  • DB:

Database

The DB folder is where I put all the code for connecting to the database and creating the necessary data collections.

  • Middleware:

Middleware

I mainly use the middleware folder to create all the middleware functions for the project. For example, after creating a JWT token, you also need to verify it. For that, I create a middleware named verifyJWT, which I can then import and use anywhere else in the project.

  • routerController:

Controllers

The routerController folder's name basically tells you its job: it holds the business logic for your API's routes.

In this project, I put both the route definitions (the actual endpoints) and their business logic (what the API does) inside this one routerController folder.

But A Better Practice (For Larger Apps):
A better practice is to separate those two jobs completely.

  • Create a separate routes folder where you only define the paths endpoint (like /users).
  • Keep your business logic separate in a controllers folder.

  • Index.js

index.js file

And finally, we have the index.js file. This is considered the main entry file for the whole server - it's the first file that runs when you start your application.

Time to Say Goodbye to the Messy index.js!

Moving away from one huge index.js file is the best way to start building serious apps. By splitting your code into organized folders, you make the project easy to understand and much simpler to grow later on. It saves you tons of headaches!

Top comments (0)