While there are no standard project templates in NodeJS like in other languages like .NET and Java, I wanted in this article to introduce a simple Boilerplate project that is using standard ES6, NodeJS Express, and connecting to a Mongo database.
You can find the boilerplate on the next link:
A NodeJS boilerplate that is using NodeJS Express, ES6 and MongoDB. It can help you quickly go up with a NodeJS APIs…
The main features of this boilerplate are:
- The use of IoC and dependency injection
- The use of middleware in a simple way
- Logging provider that supports both normal logs in addition to JSON logs
- Layered system (Controllers layer, Services layer, Repositories layer)
- Unit tests for both controllers and services
Some important points to consider while writing your Web APIs project
- You want to apply SOLID principles => You will need to apply Single responsibility => Layered system is the best point to start with, you want to know what is SOLID then check the next article https://medium.com/backticks-tildes/the-s-o-l-i-d-principles-in-pictures-b34ce2f1e898
- Express middlewares are like a pipeline of handlers, whenever you need some business logic to be applied to all requests recall middleware to achieve that (Authentication, Authorization, Logging metrics, logging traces, custom modifications on request or response, logging calls … etc).
- Dependency injection is good to have; for a layered system injecting the dependencies from outside will lead to cleaner code, more control over dependencies, and simplify unit and integration tests.
- Don’t use console.log directly, write your own logging provider even if you are using some sort of libraries, that will give you the ability to change the whole logging structure from a single file without the need to search and replace all logging commands you used inside your code.
- Don’t worry about the hard disk :) log whenever you need to, logging is very useful to detect problems later in your code, and it's a good practice to have special ids or strings that is related to the business logic you are logging, like entity id, user phone number, session-id …etc. If you are using a tracing system don’t forget also to add trace and span ids to your logs. Also, it will be good to put the source of the logs (Which class wrote that).
- Unit tests & integration tests are good to have, but maybe it would be better to keep them for complicated business logic or places that usually modified from time to time.
- A good practice would be to implement separate requests validators with a base validator class that contains the main validators you may need.
- Don’t use hardcoded strings, always define them as enums or constants.
- Don’t put hardcoded configurations like connection strings, ports, limits …etc. It will be always a good practice to inject them as environment variables when running the project.
- Your codebase should not include secrets (Passwords, certificates, connection strings … etc).
- It's good to have a base controller that handles exceptions in a unified way and send a suitable response to the caller according to the exception.
- Define your own Exceptions for every possible error and it would be great if you can categorize these exceptions.
- It would be a good practice to use a static code analysis tool like ESLint so you can catch code errors and incorrect patterns before running or committing the code.
- It would be great if you can create a base repository class that contains the most-used operations on your data entities.