When I first started building projects with Express.js, I used to put all my code into a single app.js file. It worked at the beginning, but as my projects grew, everything quickly became messy and hard to manage.

During my first year of learning, I realized that this approach doesn’t scale. That’s when I started organizing my code into a proper folder structure. It made my projects much cleaner, easier to understand, and much more maintainable.

In this article, I’ll show you the folder structure I use and explain how it can help you write better Express.js applications.



My Folder Structure

Here’s the structure I use:


📁configs
📁controllers
📁services
📁models
📁helpers
📁env
app.js

Let’s go through each part one by one.


📁 configs

The configs folder contains all project configuration files.

For example:

  • database configuration

  • environment-based settings

  • constant variables

Keeping configs in one place makes the project easier to manage and update.


📁 controllers

The controllers folder contains all the controllers of the application.

Each file represents a specific feature or endpoint group. I follow a naming convention like:

*.controller.js

For example:

auth.controller.js
user.controller.js

Controllers are responsible for handling HTTP requests and sending responses.


📁 services

The services folder contains the business logic of the application.

Instead of writing logic inside controllers, I move it into services. Controllers simply call the service methods.

I usually use classes for both controllers and services:

class AuthService {}
class AuthController {}

📁 models

The models folder is used to define database models.

These models represent your data structure and help you interact with the database. You can use them to:

  • create data

  • read data

  • update data

  • delete data

Separating models makes database logic more organized.


📁 helpers

The helpers folder is used for reusable utility functions.

For example:

  • formatting data

  • generating tokens

  • common helper logic

This helps avoid duplication and keeps your codebase clean.


📁 .env

The env folder stores environment files:

  • database URL

  • API keys

  • secret tokens

In my practice I used 3 names of env file

  • .development.env

  • .staging.env

  • .production.env

This keeps sensitive data out of your codebase and makes your app more secure.


🚀 app.js

The app.js file is the entry point of the application.

Here I:

  • initialize Express

  • configure middlewares

  • connect routes

  • connect to database

It acts as the central setup file for the app.


Conclusion

A good folder structure might seem unnecessary at the beginning, but it becomes essential as your project grows.

If you’re just starting with Express.js, organizing your code early will save you a lot of time and frustration later.

Start simple, stay consistent, and improve your structure as your projects evolve.


Տելեգրամ ալիք՝ https://t.me/TechnoBuddyArm

Medium հարթակ՝ https://medium.com/@techno.buddy.arm