Skip to content

Latest commit

 

History

History
219 lines (148 loc) · 4.32 KB

File metadata and controls

219 lines (148 loc) · 4.32 KB

🚀 Learning Fastify

Node.js Fastify MongoDB License Status Swagger OpenAPI

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.


✨ Features

  • ⚡ Fastify server with logging enabled
  • 🍃 MongoDB integration using @fastify/mongodb
  • ✅ Request & response validation using schemas
  • 🔐 Authentication using preHandler hooks (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)

🧰 Tech Stack

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

📂 Project Structure

learning-fastify/
├── server.js
├── src/
│   ├── hooks/
│   │   └── authHandler.js
│   └── routes/
│       └── user.js
├── .env.example
├── package.json
└── README.md

⚙️ Getting Started

1️⃣ Clone the repository

git clone https://github.com/OwaisZakir/learning-fastify.git
cd learning-fastify

2️⃣ Install dependencies

npm install

3️⃣ Environment configuration

Create a .env file in the root directory:

PORT=3000
MONGODB_URI=mongodb://localhost:27017/learning_fastify

⚠️ Never commit .env files. Use .env.example for reference.


4️⃣ Run the project

npm run dev

Server will start at:

http://localhost:3000

📘 Swagger API Documentation

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

📌 API Endpoints

🟢 Health Check

GET /

{
  "message": "Welcome To Auth Service!"
}

➕ Create User

POST /api/user

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "123456"
}

📄 Get All Users

GET /api/user


🔍 Search Users by Name

GET /api/user?q=john


👤 Get User by ID (Protected)

GET /api/user/:id


📤 Upload File

POST /api/upload


📚 What I Learned

  • 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

⚠️ Security Notice

  • 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

🛣️ Roadmap

  • Password hashing with bcrypt
  • JWT authentication
  • Swagger security (JWT auth)
  • Controller & service layer
  • Centralized error handling
  • Unit & integration testing

🧑‍💻 Author

Owais Zakir Backend Developer (Learning Phase 🚀)


⭐ Support

If you found this project helpful, consider giving it a ⭐ It really helps and motivates me to keep learning!