Node.js RESTful API boilerplate using Traefik, Docker, Docker Compose, Fastify, JWT and Mongodb.
- Node.js >= 20
- Docker
- Docker compose
src/
├── index.js # entry point: boot + graceful shutdown
├── app.js # builds the Fastify instance (plugins, error handling)
├── routes.js # mounts the modules under /api/v1
├── plugins/ # cross-cutting infrastructure (db, auth, sentry)
├── modules/ # one folder per business domain
│ ├── movies/ # movies.routes / .controller / .schemas / .model
│ └── users/ # users.routes / .controller / .schemas / .model
└── shared/ # generic building blocks (enums, errors, utils, validators)
The dependency rule: modules/ may import from shared/, never the other way around, and modules do not import from each other.
Install packages using docker:
docker run --rm -it \
-v ${PWD}:/usr/src/app \
-w /usr/src/app \
node:22-alpine npm iWindows users should switch the PWD variable to your current directory. Alternatively, you can run npm install as follows:
docker-compose run --rm api npm installOr if you have node installed in your system, install using npm:
npm install-
Rename the .env.example file to .env and fill variables. The SENTRY_DSN variable is not obligatory.
-
Edit your hosts file with:
127.0.0.1 fastify.localhost
In the root of project, run:
docker-compose up| Endpoint | HTTP Method | Description |
|---|---|---|
/health |
GET |
Healthcheck |
/docs |
GET |
Swagger UI |
/api/v1 |
GET |
Welcome message |
/api/v1/users/register |
POST |
Adds a new user |
/api/v1/users/auth |
POST |
Authenticate user |
/api/v1/movies |
GET |
List all movies |
/api/v1/movies/:id |
GET |
Get movie |
/api/v1/movies |
POST |
Adds a new movie |
/api/v1/movies/:id |
PATCH |
Update a movie |
/api/v1/movies/:id |
DELETE |
Delete a movie |
All /api/v1/movies routes require a Authorization: Bearer <token> header (get a token from /api/v1/users/auth). Invalid ids return 400, duplicated emails return 409, and login/register are rate limited (5 requests per minute).
The test suite uses the built-in node:test runner against an in-memory MongoDB (no Docker needed):
npm testRequest
curl -i --request GET 'http://fastify.localhost/api/v1'Response
{
"message": "Fastify API is on fire"
}I exported Insomnia collection/data for so you can test all the endpoints.
To access Traefik dashboard, simple access in your browser:
http://localhost:8080Happy coding!