Full Stack JS

Chapter 17
0%

Chapter 17: Full Backend Project

Build a complete Task Manager API from scratch, applying everything you have learned about Node.js, Express, Sequelize, and Authentication.

Project Setup
Models
Routes
Controllers
Services
Testing
Quiz

Project Setup

Building a backend project is like building a house. You need a blueprint (project structure), a foundation (setup and config), walls and rooms (models, routes, controllers), and plumbing (services and middleware). Let's build it step by step.

Architecture Diagram

Task Manager API — Architecture

Client Request
    ↓
Routes         (Define URL endpoints)
    ↓
Middleware    (Auth check, validation)
    ↓
Controllers   (Handle request/response)
    ↓
Services      (Business logic)
    ↓
Models        (Database operations)
    ↓
Database (SQLite/PostgreSQL)

Folder Structure

Project — Folder Structure

Initialize the Project

Terminal — Project Initialization
.env — Environment Variables
src/app.js — Express App Setup
server.js — Entry Point

Models

Models define your database tables. Our Task Manager has two models: User and Task.

src/models/index.js — Database Setup
src/models/User.js
src/models/Task.js

Routes

Routes define the URL endpoints your API responds to. They map HTTP methods to controller functions.

src/routes/authRoutes.js
src/routes/taskRoutes.js
Notice that router.use(authenticate) protects ALL task routes at once. Every request to /api/tasks/* must include a valid JWT token.

Controllers

Controllers handle the HTTP request and response. They receive data from the client, call the service layer, and send back a response.

src/controllers/authController.js
src/controllers/taskController.js

Services

Services contain the business logic. They interact with models and handle data processing, keeping controllers thin and focused on HTTP concerns.

src/services/authService.js
src/services/taskService.js

Testing the API

You can test your API using curl commands in the terminal, or use a tool like Postman or Thunder Client.

Terminal — Testing with curl
Pro tip: Add "dev": "nodemon server.js" to the "scripts" section of your package.json. Then npm run dev will auto-restart the server whenever you save a file.

API Endpoint Summary

MethodEndpointAuth?Description
POST/api/auth/registerNoCreate new user
POST/api/auth/loginNoLogin, get token
GET/api/tasksYesList all user's tasks
GET/api/tasks/:idYesGet one task
POST/api/tasksYesCreate a task
PUT/api/tasks/:idYesUpdate a task
DELETE/api/tasks/:idYesDelete a task

📝 Chapter 17 Quiz

1. Where does business logic belong in this architecture?

In the routes
In the controllers
In the services
In the models

2. How do we protect all task routes at once?

router.use(authenticate) before defining routes
Add authenticate to each route individually
Check the token in each controller
Put auth logic in the model

3. What is the controller's main responsibility?

Defining database table columns
Handling HTTP request/response and calling services
Hashing passwords and generating tokens
Defining URL endpoints

4. Why does the task service filter by UserId?

To make queries faster
To sort tasks by user
Because Sequelize requires it
So users can only access their own tasks

5. What does sequelize.sync() do when the server starts?

Creates database tables based on model definitions
Deletes all existing data
Connects to a remote database
Runs all pending migrations
← Chapter 16: Advanced Sequelize Chapter 18: Git & GitHub →