A boilerplate for web APIs using NodeJS
the boilerplate follows an architecture inspired by Domain Driven Design and Clean Architecture. It implies in some patterns and methodologies having to be followed to ensure separation of concerns and make the codebase more maintainable.
This boilerplate uses a folder structure and logical architecture focused on separation of concerns based in Domain-driven design and Clean architecture. Instead of the classical controllers/models/services folders, we now have layers inside the src folder. Each of the folder layers is scoped by a namespace regarding the concern it is about (like user, errors, logging and so on):
The application layer defines the actual behaviour of our application, thus being responsible for performing interactions among units of the domain layer.
In this layer, we may define units which play the role of entities and business rules and have a direct relationship to our domain This is the most isolated and important layer of our software, and it may be used by the application layer to define use cases.
This is the lowest layer of all, and it’s the boundary to whatever is external to our application: the database, email services, etc.
This layer holds all the entry points of our application, such as controllers, websockets, etc. It should not have any knowledge about business rules, use cases, persistence technologies, and not even about other kinds of logic! It should only receive user input (like URL parameters), pass it on to the use case and finally return a response to the user.
├── app
├── config
│ └── environments
├── domain
├── infrastructure
│ └── logging
└── interfaces
└── http
├── controllers
├── errors
├── logging
└── utils
Install Docker on your system.
- Install instructions for Mac OS X
- Install instructions for Ubuntu Linux
- Install instructions for other platforms
The initial (default) environment variables have been set for you in the .env
file. If you need to have custom variables in your dev process you can use a .env.local
file to override those. The application will use the following files in the following order to create a final set of variables:
.env
.env.local
.env.<environment>
.env.<environment>.local
A server (via docker container) will ONLY read the .env
file, so server specific variables should either be passed along in the build command (ENV_VAR=value <build command>
) or in the server configuration.
-
Run
npm i
. It will install all dependencies. -
Run
docker-compose build
. It will pull a base images from the Docker registry. -
Run
docker-compose up
. This will bootstrap a new app in your container optionaly add optionaly-d
Detached mode: Run containers in the background. -
Run
docker-compose down
It will Stops containers and removes containers, networks, volumes, and images created byup
.
The app should then be running on your docker daemon on the port you specified in your .env
file (default: 4050
) or override (see Prerequisites > Environment Variables)
This boilerplate comes with a collection of npm scripts to make your life easier, you'll run them with npm run <script name>
npm run start
run the application
npm run start:watch
run the application in watch mood on every file change
npm run test
run all unit tests for the appliction
npm run test:watch
run all unit tests for the appliction in watch mood on every file change
npm run test:coverage
Create a coverage report for your code and determine whether your coverage is high enough
npm run lint
Run linting on the application codebase