This directory contains complete, runnable code examples for the beginner level.
📁 File Structure
code/
├── main.ts # Application entry point
├── app.module.ts # Root module
├── app.controller.ts # Root controller
├── app.service.ts # Root service
└── todos/ # Todos feature module
├── todos.module.ts
├── todos.controller.ts
├── todos.service.ts
└── dto/
└── create-todo.dto.ts
🚀 How to Run
- Copy these files to your NestJS project
- Install dependencies:
npm install - Run:
npm run start:dev - Test the API at
http://localhost:3000
📝 API Endpoints
GET /todos- Get all todosGET /todos/:id- Get a specific todoPOST /todos- Create a new todoPUT /todos/:id- Update a todoDELETE /todos/:id- Delete a todo
🧪 Test with cURL
# Get all todos
curl http://localhost:3000/todos
# Get a specific todo
curl http://localhost:3000/todos/1
# Create a todo
curl -X POST http://localhost:3000/todos \
-H "Content-Type: application/json" \
-d '{"title":"New Todo","description":"Learn NestJS","completed":false}'
# Update a todo
curl -X PUT http://localhost:3000/todos/1 \
-H "Content-Type: application/json" \
-d '{"title":"Updated Todo","completed":true}'
# Delete a todo
curl -X DELETE http://localhost:3000/todos/1
📚 Learning Points
- Module Structure: How to organize code into modules
- Controllers: How to handle HTTP requests
- Services: How to implement business logic
- Dependency Injection: How services are injected into controllers
- DTOs: Data Transfer Objects for type safety
- HTTP Methods: GET, POST, PUT, DELETE implementation