Skip to main content

HabileLabs is headed to AWS re:Invent 2026 in Las Vegas Let's connect

Blog

Best Practice for Node.js Folder Structure

By Yogesh MishraJuly 17, 20243 min read

Best Practice for Node.js Folder Structure

Project structuring is an important topic because the way you bootstrap your application can determine the whole development experience throughout the life of the project.

Routes-

Where we define endpoints should be noun based and do not use verbs.

router.get("api/v1/get-book-list", getBookMethod); // avoid router.get("api/v1/book-list", getBookMethod); // please use

Controller -

Basic skeleton of the API should be there if the method is getting big, use a service file and move some logic there. Write reusable code and common functions.

Ex. You can’t save a web socket circular object into an app instance of express.

Organizing Files around Module

Organizing Files around Module – Best Practice for Node.js Folder Structure

Organizing Files around Module – Best Practice for Node.js Folder Structure

πŸ‘‰ Place Your Test Files Next to The Module

Tests are not just for checking whether a module produces the expected output, they also document your modules. Because of this, it is easier to understand if test files are placed next to the implementation.

Put your additional test files in a separate test folder to avoid confusion.

πŸ‘‰ Place Your Test Files Next to The Module – Best Practice for Node.js Folder Structure

πŸ‘‰ Use a config Directory

To place your configuration files, use a config directory.

πŸ‘‰ Use a config Directory – Best Practice for Node.js Folder Structure

πŸ‘‰ All shell scripts in scripts folder

Avoid writing long scripts in package.json. Create a separate file for that and use that file.

πŸ‘‰ All shell scripts in scripts folder – Best Practice for Node.js Folder Structure

πŸ‘‰ Third-party API calls and common reusable code in services folder

Third-party API integration in separate service files and maintain them in the services folder.

To sign and verify a token create a separate jwt-service and import this to sign and verify the token.

πŸ‘‰ Third-party API calls and common reusable code in services folder – Best Practice for Node.js Folder Structure

πŸ‘‰ Directory for common utils

Create a subfolder on the basis of type like server-utilities-

πŸ‘‰ Directory for common utils – Best Practice for Node.js Folder Structure

Let’s assume we are dealing with Postgres, then-

πŸ‘‰ DB directory for database-related stuff (MongoDB or Postgres) – Best Practice for Node.js Folder Structure

πŸ‘‰ Request validation : express-validator

Add an express-validator to validate and sanitize the request. Validation should be added on the model level and on the request level.

πŸ‘‰ Request validation : express-validator – Best Practice for Node.js Folder Structure

πŸ‘‰ Do not write every constant in the .env file

Only server-related essential credentials should be there for third parties etc.

Create a separate file and read from there like environment.json or put them in constants.js in utilities.

"doc": "apidoc -i app/ -o client/dist/eps-client/doc", and npm run doc apidoc.json will be in parallel of package.json

Share:
0
+0