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).
Here are the available routes for performing CRUD operations on customers:
-
Create a Customer (POST)
curl -X POST http://localhost:9999/v1/customers \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "phone": "123456789"}'
-
Retrieve All Customers (GET)
curl http://localhost:9999/v1/customers
-
Retrieve a Customer by ID (GET)
curl http://localhost:9999/v1/customers/<customer_id>
-
Update a Customer (PUT)
curl -X PUT http://localhost:9999/v1/customers/<customer_id> \ -H "Content-Type: application/json" \ -d '{"name": "Jane Doe", "phone": "987654321"}'
-
Delete a Customer (DELETE)
curl -X DELETE http://localhost:9999/v1/customers/<customer_id>
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