Loading Depth Study
Preparing your workspace
Routing defines how an Express application responds to requests for specific URLs and HTTP methods.
Express provides methods such as app.get, app.post, app.put, app.patch, and app.delete for creating routes. A route combines an HTTP method with a path and a handler function. Route parameters are useful when a resource identifier is part of the URL, while query parameters are useful for optional filtering, sorting, and pagination. Clear route design makes APIs easier to understand and maintain.
A course API can use GET /courses to list courses, GET /courses/:id to get one course, and POST /courses to create a new course.
import express from 'express';
const app = express();
app.get('/courses', (req, res) => {
res.json({ message: 'List courses' });
});
app.get('/courses/:id', (req, res) => {
res.json({ courseId: req.params.id });
});
app.post('/courses', (req, res) => {
res.status(201).json({ message: 'Course created' });
});The application responds differently depending on the HTTP method and requested URL.