Lesson 18 of 30

Express Routing

Chapter 18

Express Routing

Learn how to define routes and handle different URLs in Express.

Theory

Routing determines how an application responds to different URLs and HTTP methods. Express provides methods such as: • app.get() • app.post() • app.put() • app.patch() • app.delete() Routes can also contain dynamic parameters.

Example

Node.js Example
const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Home");
});

app.get("/about", (req, res) => {
  res.send("About");
});

app.listen(3000);

Output

Different URLs return different responses.

Practice

Create GET routes for home, about, services, and contact pages.

Interview Questions

1.

What is routing?

2.

What does app.get() do?

3.

How do you create a POST route?

Quick Quiz

Which option best matches this chapter?

A.Routing
B.Database indexing
C.CSS styling
D.File compression

Answer: Routing