A Fastify + MongoDB learning project focused on understanding Fastify core concepts such as hooks, schemas, plugins, and request lifecycle.
This repository is part of my backend learning journey using modern Node.js tools.
- ⚡ Fastify server with logging enabled
- 🍃 MongoDB integration using
@fastify/mongodb - ✅ Request & response validation using schemas
- 🔐 Authentication using
preHandlerhooks (learning demo) - 📦 Modular route structure
- 🔍 Simple CRUD APIs for users
- 📤 File upload using
@fastify/multipart - ♻️ Auto-reload during development with Nodemon
- 📘 Interactive API documentation using Swagger (OpenAPI)
| Technology | Description |
|---|---|
| Node.js | JavaScript runtime |
| Fastify | Fast & low-overhead web framework |
| MongoDB | NoSQL database |
| @fastify/mongodb | MongoDB plugin for Fastify |
| Swagger (OpenAPI) | API documentation |
| Nodemon | Development auto-restart tool |
learning-fastify/
├── server.js
├── src/
│ ├── hooks/
│ │ └── authHandler.js
│ └── routes/
│ └── user.js
├── .env.example
├── package.json
└── README.mdgit clone https://github.com/OwaisZakir/learning-fastify.git
cd learning-fastifynpm installCreate a .env file in the root directory:
PORT=3000
MONGODB_URI=mongodb://localhost:27017/learning_fastify
⚠️ Never commit.envfiles. Use.env.examplefor reference.
npm run devServer will start at:
http://localhost:3000
Interactive API documentation is available via Swagger UI.
http://localhost:3000/docs
You can:
- View all available endpoints
- Inspect request & response schemas
- Test APIs directly from the browser
GET /
{
"message": "Welcome To Auth Service!"
}POST /api/user
{
"name": "John Doe",
"email": "john@example.com",
"password": "123456"
}GET /api/user
GET /api/user?q=john
GET /api/user/:id
POST /api/upload
- Fastify request lifecycle and hooks (
preHandler) - Difference between Fastify hooks and Express middleware
- Schema-based validation (body, params, query)
- MongoDB CRUD operations
- File uploads using
@fastify/multipart - Swagger (OpenAPI) integration
- Error handling patterns in Fastify
- Passwords are stored as plain text
- Authentication uses a dummy token validator
- This project is ONLY for learning purposes
In real-world applications:
- Hash passwords using bcrypt
- Use JWT authentication
- Add role-based access control
- Password hashing with bcrypt
- JWT authentication
- Swagger security (JWT auth)
- Controller & service layer
- Centralized error handling
- Unit & integration testing
Owais Zakir Backend Developer (Learning Phase 🚀)
- GitHub: @OwaisZakir
If you found this project helpful, consider giving it a ⭐ It really helps and motivates me to keep learning!