No Omnipotent of ExpressJS Project Skeleton
— Generally, There is no official & omnipotent express js project structure or project skeleton.
The NodeJS communities have different style and preference to organiza the project structure or usually called as nodejs project boilerplate. Each organization or lead developer might have their own business requirement to met. Resulting in varies available express js project boilerplate or folder skeleton that available on the internet.
However, eventhough there is no omnipotent best practices set on the express js framework, any developer lead should set a standard project skeleton on their organization to increase the productivity of their team.
Project Skeleton References
below is one of nodejs product structure references.
project-root/
├── .git/
├── .gitignore
├── .env
├── package.json
├── server.js
│
├── src/
│ ├── config/
│ │ ├── index.js
│ │ └── index.test.js
│ │
│ ├── middleware/
│ │ ├── auth.middleware.js
│ │ ├── auth.middleware.test.js
│ │ ├── error.middleware.js
│ │ └── error.middleware.test.js
│ │
│ ├── utils/
│ │ ├── db.js
│ │ ├── db.test.js
│ │ ├── date.util.js
│ │ ├── date.util.test.js
│ │ ├── file.util.js
│ │ └── file.util.test.js
│ │
│ ├── services/
│ │ ├── user/
│ │ │ ├── model/
│ │ │ │ ├── user.model.js
│ │ │ │ └── user.model.test.js
│ │ │ ├── dto/
│ │ │ │ ├── user.dto.js
│ │ │ │ └── user.dto.test.js
│ │ │ ├── dao/
│ │ │ │ ├── user.dao.js
│ │ │ │ └── user.dao.test.js
│ │ │ ├── service/
│ │ │ │ ├── user.service.js
│ │ │ │ └── user.service.test.js
│ │ │ ├── interface/
│ │ │ │ ├── user.interface.js
│ │ │ │ └── user.interface.test.js
│ │ │ └── route/
│ │ │ ├── user.route.js
│ │ │ └── user.route.test.js
│ │ │
│ │ ├── post/
│ │ │ ├── model/
│ │ │ │ ├── post.model.js
│ │ │ │ └── post.model.test.js
│ │ │ ├── dto/
│ │ │ │ ├── post.dto.js
│ │ │ │ └── post.dto.test.js
│ │ │ ├── dao/
│ │ │ │ ├── post.dao.js
│ │ │ │ └── post.dao.test.js
│ │ │ ├── service/
│ │ │ │ ├── post.service.js
│ │ │ │ └── post.service.test.js
│ │ │ ├── interface/
│ │ │ │ ├── post.interface.js
│ │ │ │ └── post.interface.test.js
│ │ │ └── route/
│ │ │ ├── post.route.js
│ │ │ └── post.route.test.js
│ │ │
│ │ └── comment/
│ │ ├── model/
│ │ │ ├── comment.model.js
│ │ │ └── comment.model.test.js
│ │ ├── dto/
│ │ │ ├── comment.dto.js
│ │ │ └── comment.dto.test.js
│ │ ├── dao/
│ │ │ ├── comment.dao.js
│ │ │ └── comment.dao.test.js
│ │ ├── service/
│ │ │ ├── comment.service.js
│ │ │ └── comment.service.test.js
│ │ ├── interface/
│ │ │ ├── comment.interface.js
│ │ │ └── comment.interface.test.js
│ │ └── route/
│ │ ├── comment.route.js
│ │ └── comment.route.test.js
│ │
│ └── routes.js
│ └── routes.test.js
Project initialization
at this stage, a baseline of expressjs project should be populated.
- .git file intitiated
- package.json
- .gitignore (important, keep the source code on the repositories as minimal as possible and has no dependency with developer configuration)
- .env - environemnt variables, keep all configuration on this file such as endpoint url, port, etc. do not store sensitive security information here
- setup secure key management outside express. for example azure keyvault, etc.
component number 1 until 4 should be added on the project root level.
Source Code
typically there are five type of source code
- Services (business logic!)
- Services/Model
- Services/DTO (Data transfer object)
- Services/DAO (Data access object)
- Services/Interfaces
- Route (controller logic when handling route)
depending on the project requirement and dev lead preferences, each of the source code type should have their own directory under src folder.
— business logic should be put on the services folder.
server.js or app.js(any name will do) should be the file name to intiate the express instances.
Middleware
all middleware used on the project should stored on dedicated folder under src and injected on server.js
for example, lets say there are 3 route (/, /news and /admin). inject the authenticatio middleware on the server.js at same time when importing and activating the route to keep the code simple and clean.
note: it can also be separated from server/app.js and then imported to the server/app.js then inject it to each of the applicable route.
Utils
All utilities functionality which can be reused by services or dao should be placed under utils folder inside src folder.
for example:
- database connection initilization
- date formatter
- file manager
- and so on..
Test File for Unit Testing
it is recommended to have test file with .test.js prefixes.
each of source code that require unit test should have corresponding .test.js file