This project demonstrates how to perform CRUD operations using Node.js with the Fastify framework and MongoDB. It includes unit tests that verify the functionality of the API endpoints and track code coverage.
- Docker and Docker compose
- Node.js (version 20 or later)
- MongoDB (local or cloud instance)
-
Clone the repository:
git clone https://github.com/ErickWendel/nodejs-fastify-mongodb-crud.git cd nodejs-fastify-mongodb-crud -
Install the dependencies:
npm ci
To run the tests and see code coverage, use:
docker-compose up -d mongodb
npm testThis will execute the tests defined in the project and provide a coverage report.
To initialize the MongoDB, run:
docker-compose up -d mongodbTo initialize the project, run:
npm startThe server will start, and you can access the API on http://localhost:9999 (or your specified port).
This API uses JWT-based authentication. All routes except GET /v1/health require a valid Bearer token.
Two roles are available:
| Role | Username | Password | Permissions |
|---|---|---|---|
| admin | erickwendel | 123123 | read, create, update, delete |
| member | ananeri | 1234 | read only |
The /v1/auth/service-token endpoint requires valid user credentials plus the adminSuperSecret. It returns the user's role and a unique UUID service token.
The adminSuperSecret is: AM I THE BOSS?
# As admin
curl -X POST http://localhost:9999/v1/auth/service-token \
-H "Content-Type: application/json" \
-d '{"username": "erickwendel", "password": "123123", "adminSuperSecret": "AM I THE BOSS?"}'
# As member
curl -X POST http://localhost:9999/v1/auth/service-token \
-H "Content-Type: application/json" \
-d '{"username": "ananeri", "password": "1234", "adminSuperSecret": "AM I THE BOSS?"}'Response:
{ "role": "admin", "serviceToken": "550e8400-e29b-41d4-a716-446655440000" }Store the service token and use it as a Bearer token for API calls:
export SERVICE_TOKEN="<uuid-from-response>"
curl http://localhost:9999/v1/customers \
-H "Authorization: Bearer $SERVICE_TOKEN"
⚠️ Service token requests are rate limited to 3 requests per minute per token. Exceeding the limit returns429 Too Many Requests.
curl -X POST http://localhost:9999/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "erickwendel", "password": "123123"}'curl -X POST http://localhost:9999/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "ananeri", "password": "1234"}'Both return:
{ "token": "<jwt-token>" }Store the token and pass it as a Bearer header on subsequent requests:
export TOKEN="<jwt-token>"All protected routes require the Authorization: Bearer <token> header.
-
Health check — public, no token needed (GET)
curl http://localhost:9999/v1/health
-
Create a Customer — admin only (POST)
curl -X POST http://localhost:9999/v1/customers \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '{"name": "John Doe", "phone": "123456789"}'
-
Retrieve All Customers — admin & member (GET)
curl http://localhost:9999/v1/customers \ -H "Authorization: Bearer $TOKEN" -
Retrieve a Customer by ID — admin & member (GET)
curl http://localhost:9999/v1/customers/<customer_id> \ -H "Authorization: Bearer $TOKEN"
-
Update a Customer — admin only (PUT)
curl -X PUT http://localhost:9999/v1/customers/<customer_id> \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '{"name": "Jane Doe", "phone": "987654321"}'
-
Delete a Customer — admin only (DELETE)
curl -X DELETE http://localhost:9999/v1/customers/<customer_id> \ -H "Authorization: Bearer $TOKEN"
This project is licensed under the MIT License - see the LICENSE file for details.
- Fastify - Fast and low-overhead web framework for Node.js
- MongoDB - NoSQL database for storing data
- Node.js Test Runner - Built-in test runner for Node.js