Chapter 2: Learn a Framework

You can theoretically build an entire web server from absolute scratch using raw Python or raw Node.js. You would have to manually parse incoming HTTP headers, manually extract URL parameters, and manually format JSON responses.

Do not do this. It is a waste of time. Backend Frameworks exist to handle the boring infrastructure so you can focus on writing your actual business logic.

Popular Frameworks by Language

Routing: Directing Traffic

The main job of a framework is Routing. When a user sends an HTTP GET request to /users, your code needs to execute a specific function to fetch the users from the database.

// Express.js Example
const express = require('express');
const app = express();

// Route 1
app.get('/users', (req, res) => {
    res.json({ message: "Here are all the users!" });
});

// Route 2
app.post('/login', (req, res) => {
    res.json({ message: "Logging you in..." });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Middleware: The Security Guards

Middleware are functions that run before the final route handler. Imagine them as security guards at a club.

If someone tries to access the /admin route, you don't want to write authentication logic inside the route itself. Instead, you create an isAuthenticated middleware. The request hits the middleware first. If the user is fake, the middleware rejects them instantly. If they are real, it passes them through to the route.

MVC Architecture

As your app grows, you must organize your code using the Model-View-Controller (MVC) pattern. Models handle the Database. Controllers handle the business logic (if password == correct). Views (or JSON responses) handle what the user sees. Keep them strictly separated!

Mini Task: Run a Local Server

  1. Initialize an empty Node project: npm init -y
  2. Install Express: npm install express
  3. Copy the Express.js code block from above into an index.js file and run it. You just built a server!
Continue to Chapter 3: Databases