Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 14, 2022 11:19 am GMT

How to structure your Express and Node.Js project

Why is project architecture so crucial?

Having a proper foundation for our project architecture is critical for the project's longevity as well as your capacity to react to changing needs in the future. A faulty, unorganized project architecture frequently results in:

  • Unreadable and messy code slows down development and makes testing the product more difficult.
  • Repetition is inefficient and makes code more difficult to maintain and manage.
  • It's difficult to add new features without breaking old code.

The primary goal of any Node.js project structure is to assist you in:

  • Create code that is both tidy and readable.
  • Create code that can be reused across our program.
  • Repetition should be avoided.
  • New features can be added without affecting current code.

There is no correct or wrong way !

In the end, I don't think there's a single optimum project structure for Express projects.

Rather than asking, "How should I organize my files and folders?"
I believe it is more appropriate to ask, "Where do my various sorts of logic go?"
There are more specific answers to that issue, as well as steps we may take.
And, if we do a good job of layering our logic, a project structure will develop automatically.
A structure that can be organized in whatever way you want, whether it's using more classic MVC (or MVC-ish) or the cool new kid, components. This is due to the fact that these structures will be layered in either case! Simply place the routes, controllers, services, and other items in a component folder.

The idea is to use the principle of separation of concerns, to move the business logic away from the node.js API Routes.

Here's what i would suggest :

   app.js          # App entry pointroutes          # Our routes controllers for all the endpoints of the appconfig          # Environment variables and configuration related stuffcontrollers     # Functions for our APIsmodels          # Database modelsmiddlewares     # Contains all the middleware that we needutils           # Common functions that would be used repetitively

Let's get right in and learn more about the project's structure.

  • /Controllers- This folder would contain all the functions for your APIs. Naming of files- xxxxx.controllers.js

  • /Routes- This folder would contain all the routes that you have created using Express Router and what they do would be exported from a Controller file
    Naming of files- xxxxx.routes.js

  • /Models- This folder would contain all your schema files and and the functions required for the schema would also lie over here.
    Naming of files- xxxxx.js

  • /Middleware- This folder would contain all the middleware that you have created, whether it be authentication/some other function.
    Naming of files- xxxxx.middleware.js

  • /Utils(Optional)- The common functions that you would require multiple times throughout your code
    Naming of files- Normal project file naming scheme

  • /Templates(Optional)- If your code requires you to send certain emails/ HTML code to the client-side, store it in this files
    Naming of files- Normal project file naming scheme

  • /Config(Optional)- Configuration files for third party APIs/services like amazon s3 or db connections ...

Files in the root of your project

  • app.js- This file would basically be the entry point of the Express application and should be as minimal as possible
    package.json- file which contains all the project npm details, scripts and dependencies.

  • .gitignore- The files you dont want to push to git
    Enough talking, lets see how the project structure would actually look like with some example files

  • .env- This file is where you store all your secret keys, it should never be pushed to your github repo !

That is pretty much it, you are now ready to make your own Express project from scratch !


Original Link: https://dev.to/nermineslimane/how-to-structure-your-express-and-nodejs-project-3bl

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To