DEV Community

Cover image for Folder Structure for NodeJS & ExpressJS project

Folder Structure for NodeJS & ExpressJS project

Vaibhav Mehta on October 31, 2022

Prelude I've worked over several backend technologies, starting from PHP, then moving to RoR before getting my hands on NodeJS. I love h...
Collapse
 
xr0master profile image
Sergey Khomushin

I think it's not a secret for anyone that express.js is a dead project. Gets some packages updated every half of the year, and it hasn't changed in 5 years.
Outdated, and slow, with a whole bunch of legacy code supporting very old node versions. Why do you need It?

I highly recommend not making a monolith when your client side and server side are together in the same project. Create two projects and use the API.

Collapse
 
akshayjp11 profile image
Akshay Patil

I disagree that express is dead. Are you even up with the latest frameworks like NestJS, NextJS etc.. which have over a million downloads a week on npm registry? They all use express under the hood. Just because you’re not using it directly doesn’t mean it is dead. There are 65k dependent packages as we speak.

Collapse
 
Sloan, the sloth mascot
Comment deleted
 
akshayjp11 profile image
Akshay Patil

Lol Okay!

Collapse
 
mr_ali3n profile image
Vaibhav Mehta

How is Expresssjs a dead project? They have been pushing out updates regularly.

Secondly I've stated that the above could be used as a website and as a standalone project if it's API only. The only thing you need to do is get rid of public and src like folders which are not relevant. Rest stays pretty much the same.

Collapse
 
xr0master profile image
Sergey Khomushin

4.17.1 - May 26, 2019
4.17.2 - Dec 17, 2021
4.17.3 - Feb 17, 2022
4.18.0 - Apr 25, 2022
4.18.2 - 24 days ago

Incredibly regular updates for 4 years, you're right. 90% of new "features" with the prefix "deps". The remaining 10% are minor issues. But I do not dissuade you, use what is convenient for you. Some people still use jQuery and are quite happy with it.

Thread Thread
 
mr_ali3n profile image
Vaibhav Mehta

Some people still use jQuery and are quite happy with it.

What's wrong with jquery either? Not everything requires react or angular.

Express is pretty stable in its current state. I would rather see it as a framework which is matured enough that need not have to be updated as frequently as it doesn't break.

Look at the issues they have closed over the time, so what makes you think it isn't maintained? I am not against using something new or latest but a lot of devs these days want to use latest shiny frameworks without understanding the real use case.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
mr_ali3n profile image
Vaibhav Mehta

Agree to your comment that we belong to different categories so our thoughts won't align so let's leave this discussion here.

Collapse
 
joedotnot profile image
joedotnot

Nice answer, without even reading the article.

Collapse
 
ajmas profile image
Andre-John Mas

What do you recommend in its place and why? How is it no longer doing the job that people use it for?

Collapse
 
mr_ali3n profile image
Vaibhav Mehta

Seems like your comment aged like milk.

Collapse
 
xr0master profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Sergey Khomushin

Right, devs from India can breathe freely now.
5 years of tense anticipation.

Thread Thread
 
mr_ali3n profile image
Vaibhav Mehta

Seems like you don't have anything so good job on passing a racist comment 🤣

Thread Thread
 
xr0master profile image
Sergey Khomushin

Only racists see racism everywhere.

Collapse
 
mrlazyprogrammer profile image
Smruti Ranjan Badatya

It's very wrong to say Express is dead. A lot of companies have used NodeJS in their systems { although the usage may be limited }.

Collapse
 
xr0master profile image
Sergey Khomushin

Express != NodeJS

Thread Thread
 
mrlazyprogrammer profile image
Smruti Ranjan Badatya

Node + Express it is. I missed it I think while writing.

Collapse
 
mrrishimeena profile image
Rishi Kumar

If express.js is dead! Then earth is flat too.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Stop using dotenv. Use wj-config. It is far better.

Collapse
 
subfuzion profile image
Tony Pujals • Edited

Not to be super critical, but you should probably disclose that you're the only contributor to wj-config, a project that's only a few months old, when you promote your own work. Telling someone to stop using something because your creation is "far better" as a single, isolated statement without justification, context, or evidence, is self-biased.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Yes, it is self-biased. I, however, would not invest time in trying to solve something that already has an acceptable solution. I come from the .Net environment where configuration is far more robust. When I had the need for configuration in the JavaScript realm it was a shock for me. I could not stand the current configuration solutions, so I made my own.

As far as being the only contributor or being a few months old, I don't see the relevance. The product can speak for itself, regardless of who made it, I would say.

Thread Thread
 
subfuzion profile image
Tony Pujals

It might not seem relevant to you as the author -- it's certainly relevant to everyone else in the community when you express authoritative (and biased, self-promoting) comments and tell somebody what to do (or stop doing), as you did. A bit of self-awareness as well as a little tact toward the author of the post you commented on is healthy, friend.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

Ah, I see now what you meant. It is not about the capability of the package, it is about the social aspect of the sentence. I just wrote that quickly, as I do most comments, without a second thought.

I get it. I meant no disrespect towards the author. I simply volunteered an option without much second thought.

Collapse
 
mr_ali3n profile image
Vaibhav Mehta

What problem does it solve in the above context as opposed to using dotENV?

Collapse
 
webjose profile image
José Pablo Ramírez Vargas • Edited

It will allow you to consolidate your configuration into a single hierarchical calculated source as opposed to having to have multiple JS configuration files. It will support great flexibility regarding the source of your configuration, and on top of that, will keep your configuration much DRY'er.

Finally, as the cherry on top, if you are proxying or depending on other HTTP servers, wj-config will provide URL functions based on URL configuration data that can do route value replacement and dynamic query string addition. All replacements are URL encoded for you.

So, to answer your question, I'd say it solves problems you don't even know you have. Let me know if you are curious. I have a live demo in React @ CodeSandbox. Cheers.

Thread Thread
 
mr_ali3n profile image
Vaibhav Mehta • Edited

Thank you for explaining. With due respect, no offence to your solution but I think dotENV would do a fair job here. I am aligned on what @subfuzion mentioned earlier, I had checked the details of the package before I asked you the question as well as your posts which were mostly on wj-config but I feel your package is not relevant to the article I've written.

That being said, I'll see if I could use it in some of the projects if needed :)

Thanks for the suggestion though.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

No problem. To summarize on my intent, what I disliked due to my very own personal experience with dotenv and your proposed folder structure is the need to have independent configuration sources. If you split the configuration, you split the maintenance too.

Since dotenv is not hierarchical in nature, it makes it very difficult and cumbersome to keep maintaining projects with it, of course, when compared to more powerful configuration approaches like .Net Configuration. Since I was able to bring the power of .Net Configuration to the JavaScript world, I figured I dropped the link here.

If you were to use wj-config, you would quickly realize that your configuration folder would become simplified. That was the story behind the recommendation.

Collapse
 
lexpeee profile image
Elex

I find this very insightful. I don't really understand why people would keep suggesting and/or tell people to use a different library than the ones mentioned in this article, but I totally get that there are libraries that are better than the ones we use, but this focuses on the stuff that the majority of the developers/engineers use. I also do think this post is usually for those who want to learn and understand and gain different approaches and perspective from different people in terms of file architecture.

Collapse
 
610470416 profile image
NotFound404

Use aex instead of express

Collapse
 
ajmas profile image
Andre-John Mas

What problem is aex solving that express doesn’t? What now the only thing I can see it is pushing the use of decorators, which feels more like something a Java insisted on.

What’s your relationship to aex?

Collapse
 
610470416 profile image
NotFound404

Just take a look at:

github.com/calidion/aex

It has at lease the following advantages over express:

  1. Web Straight Line theory backed.
  2. Object Oriented support instead of callback functions only.
  3. Better Error support
  4. Ease way to separate your business code from web processing code
  5. Simple processing for web instead of writing repeating code again and again for getting body, query, params, and their data integrity.
  6. Simple way to retrieve files.
  7. Websocket support
  8. Separate data from web process with scope variable.

And 20% faster than express.

Thread Thread
 
ajmas profile image
Andre-John Mas • Edited

Are you the author of aex?

AEX is the only project that makes reference to “web straight line”, from what I can see. Also articles on it on medium don’t have any feedback, making me nervous about something that doesn’t have a proper discussion.

Thread Thread
 
610470416 profile image
NotFound404 • Edited

What is wrong if I am the author? So you determine things by medium not logic?

Thread Thread
 
ajmas profile image
Andre-John Mas • Edited

There is nothing wrong if you are the author, just that you aren’t being transparent that you are. It’s like recommending stocks, while not disclosing you are the owner of the business.

It helps us in deciding how objective an opinion is.

Thread Thread
 
610470416 profile image
NotFound404

So you don't recognize thing by your brain only by listening to others?

Collapse
 
610470416 profile image
NotFound404

Decorator is only a simple way to implement Web Straight Line.
Aex follows Web Straight Line instead of MVC which we think it is not suitable for web.

Collapse
 
yeasin2002 profile image
Md Kawsar Islam Yeasin

I don't know way I don't love this kind of folder structure where route, controller are not at the same file

Here What I prefer:

`

project-root/

├── src/
│ ├── api/ # Group controllers, routes, and validation by feature
│ │ ├── user/
│ │ │ ├── user.controller.ts # User controller
│ │ │ ├── user.route.ts # User routes
│ │ │ ├── user.validation.ts # User input validation (optional)
│ │ │ └── user.service.ts # User-specific services
│ ├── database/
│ │ ├── Redis.database.js
│ │ ├── Mongo.database.js
│ │ └── auth/
│ │ ├── auth.controller.ts # Auth controller
│ │ ├── auth.route.ts # Auth routes
│ │ ├── auth.service.ts # Auth service
│ │ └── auth.validation.ts # Auth validation (optional)
│ │
│ ├── config/ # App configuration (environment, database, etc.)
│ │ ├── database.ts # Database connection
│ │ ├── env.ts # Environment variable configuration
│ │ └── logger.ts # Logger configuration
│ │
│ ├── middlewares/ # Custom middleware (authentication, error handling)
│ │ ├── error.middleware.ts # Centralized error handling
│ │ ├── auth.middleware.ts # Auth middleware for protected routes
│ │ └── validate.middleware.ts # Validation middleware for request schemas
│ │
│ ├── models/ # Mongoose/Sequelize models or DB schemas
│ │ ├── user.model.ts # User model (Mongoose, Sequelize, etc.)
│ │ └── auth.model.ts # Auth-related model (tokens, sessions, etc.)
│ │
│ ├── services/ # Business logic and reusable services
│ │ ├── email.service.t # Email service (send emails)
│ │ ├── auth.service.ts # Authentication and authorization service
│ │ └── user.service.ts # User-related services (CRUD operations)
│ │
│ ├── utils/ # Helper functions/utilities (non-business logic)
│ │ ├── httpResponse.ts # Standardized response format
│ │ ├── constants.ts # App constants
│ │ └── hash.ts # Password hashing utility
│ │
│ ├── validations/ # Centralized validation schemas (using Zod, Joi, etc.)
│ │ ├── user.validation.ts # User-related validation
│ │ └── auth.validation.ts # Auth validation
│ │
│ ├── app.ts # Initialize Express app
│ └── index.ts # Main entry point to start the server

├── dist/ # Compiled JavaScript files (from TypeScript)

├── node_modules/ # Dependencies

├── .env # Environment variables
├── .eslintignore # ESLint ignore patterns
├── .eslintrc.json # ESLint configuration
├── .gitignore # Ignore node_modules and dist
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md

`

Collapse
 
mrlazyprogrammer profile image
Smruti Ranjan Badatya

For the most part, you have a great structure. I have something to add on to the structure. Basically, we can follow something called domain-driven development. By doing this we can make the codebase a lot more maintainable. You can read about it in this blog post.

Scalable Directory Structure for NodeJS + Express Web Servers

Collapse
 
johnram98468527 profile image
JohnRamsey

It was interesting to read your article and learn something new. I myself now work in a company and do database management. I recently had a problem, I didn't know how to quickly sync data in MySQL. So, I came across this article about sync MySQL databases Where I found a detailed guide on how to do everything. Perhaps someone will also find it useful, so I'll leave it here.

Collapse
 
shubham_poddar_6649a42d26 profile image
Shubham Poddar

The folder structure for a Node.js and Express.js project helps keep your code organized by grouping related files together. It has separate folders for configuration, handling routes, database models, middleware, and other important parts of your app. This makes it easier to find and manage your code as your project grows.

Collapse
 
shagunsharma6677 profile image
Shagun Sharma

They @mr_ali3n can you provide template of this nodejs folder structure. It would be really helpfull.

Thank You.

Collapse
 
shruti_rai profile image
Shruti Rai

Great explanation

Collapse
 
aakanksha_kumari_23 profile image
Aakanksha Kumari

Thanks for sharing these best practices. It’s always helpful to see how experienced developers structure their projects to keep things manageable.

Collapse
 
shruti_rai profile image
Shruti Rai

Great article!

Collapse
 
navaneetha_krishnan_b9695 profile image
Navaneetha Krishnan

i am creating the mern stack project give me the folder structure

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
somsubhra1 profile image
Somsubhra Das

test

Collapse
 
raunakkk profile image
Raunak Agarwal

Well said

Collapse
 
abhinavraghav4it profile image
Abhinav Raghav • Edited

Interesting perspective on Express.js! While some argue it’s outdated, its continued use in many modern frameworks suggests it still has a strong presence in the ecosystem.

Collapse
 
aditi_shukla_9bbf207a15ca profile image
Aditi Shukla

A detailed explanation. It taught me a lot as a NodeJS begginer.

Collapse
 
priyanshi_siwach profile image
Priyanshi Siwach

This folder structure seems easy to follow, clean and manageable. Thanks for this!

Collapse
 
dinesh_41916 profile image
Kuddana Dinesh

A good folder structure in your Node JS and Express JS project helps keep your code organized and easy to debug. It makes your project clearer and simpler to work with. Thank you for this blog!

Collapse
 
shruti_tagade profile image
Shruti Tagade

Well explained! The structure is clear and easy to follow. Thanks for sharing!

Collapse
 
narendranegi profile image
Neeraj negi

🙂 nice

Collapse
 
sonali_dora profile image
K. Sonali Dora

Very useful one.

Collapse
 
sampritym profile image
Samprity Mukherjee

very useful

Collapse
 
esha_lal profile image
Esha Lal

Your guide promotes a clear and structured approach to Node.js project organization, making it easier for developers to maintain and scale their applications.

Collapse
 
vinay_yadam_e3e2913714ffd profile image
vinay yadam

Useful article, especially for beginners.