This repository was developed while following Fernando Herrera's Node.js course. It focuses on the implementation of a REST API using Express, MongoDB, and JSON Web Tokens (JWT), covering authentication, authorization, input validation, and CRUD operations.
Although the original course uses JavaScript, this project was later migrated to TypeScript as a personal extension to improve type safety and maintainability while preserving the original API behavior.
The API includes users, roles, categories, products, and search endpoints, providing a practical backend architecture and a solid foundation for REST API development.
- Authentication with JSON Web Tokens (JWT).
- Role-based authorization for protected endpoints.
- Input validation with
express-validator. - CRUD operations for users, categories, and products.
- Search across users, categories, and products.
- Pagination for list endpoints.
The complete OpenAPI 3.0 specification is available in docs/openapi.yaml. It documents every endpoint, request body, response, validation rule, and authentication requirement implemented by the API.
You can inspect or import the specification with any OpenAPI-compatible tool, including Swagger Editor, Postman, and Insomnia.
Prerequisites: Node.js 20+, npm, and MongoDB (local or Atlas).
- Clone this repository and install dependencies with
npm ci. - Copy
.env.exampleto.envand set all variables:PORT: HTTP port (defaults to3000when omitted).MONGODB_CNN: MongoDB connection string.SECRETORPRIVATEKEY: long, random secret used to sign JWTs.
- Create role documents in MongoDB before registering users. The API expects roles named
admin_roland/orventas_rol; use each role document's_idas therolfield when creating a user. - Run the application with
npm run dev.
The server is available at http://localhost:3000 by default. Never commit .env or real credentials.
| Resource | Endpoint | Description |
|---|---|---|
| Authentication | POST /api/auth/login |
Authenticates a user and returns a JWT. |
| Users | /api/usuarios |
Lists, creates, updates, and soft-deletes users. |
| Categories | /api/categorias |
Lists, creates, updates, and soft-deletes categories. |
| Products | /api/productos |
Lists, creates, updates, and soft-deletes products. |
| Search | GET /api/buscar/:coleccion/:termino |
Searches users, categories, or products. |
List endpoints support pagination through query parameters:
- Users and categories:
?limite=5&desde=0 - Products:
?limit=10&from=0
The parameter names are currently inconsistent between resources and will be standardised in a future iteration.
This project does not currently have a maintained automated test suite. The existing test is a legacy integration test that depends on pre-existing MongoDB data and may write a product, so it is not a reliable verification step for a fresh clone. It should not be treated as evidence of test coverage until it is isolated with dedicated fixtures and a test database.
If you have any questions or suggestions, please don't hesitate to contact me at calvarez.brc@gmail.com