Well-structured backends separate concerns: routers define URLs, controllers translate HTTP to application calls, services own data access, and middleware handles cross-cutting behavior (validation, 404, errors). Study these patterns to keep files small, testable, and aligned with REST conventions.
Query and route parameters
Matches the course lesson Query and route parameters. Covers req.params (path segments like :userId) and req.query (?sort=desc)—two different inputs controllers read before calling services. See the lesson overview, then route vs query topics below.
Chegg Skills Lesson 5 (Node and Express): read three request inputs—path params, query string, and body—and use each in the right place in controllers and services.
Learn: Lesson 5 in the Node and Express unit introduces the three ways HTTP sends data into your API. Route parameters identify which resource (req.params). Query parameters optional filters (req.query). The body carries JSON on POST/PUT (req.body). Controllers read all three; routers only declare path parameter names in the URL pattern.
Course example
// Path parameter (router defines :userId)
// GET /users/42 -> req.params.userId === "42"
// Query string (optional filters)
// GET /pastes?sort=desc&limit=5 -> req.query.sort, req.query.limit
// JSON body (POST/PUT, needs express.json())
// POST /users body: { "username": "ada" } -> req.body.username
Path segments like :userId and :pasteId are available on req.params. Define them in the router path; read them in the controller or service.
Learn: Route parameters are part of the path template. In /users/:userId/pastes/:pasteId, req.params.userId and req.params.pasteId are strings set by Express when the route matches. Use them to identify which resource to load or update. With nested routers, mergeParams: true lets a child router see :userId from the parent mount.
Optional filters and flags after ? in the URL (e.g. ?sort=desc&limit=10) are on req.query. Use for sorting, pagination, and search—not for resource IDs.
Learn: Query parameters are optional and unordered. They are ideal for ?sort=name&limit=20 or ?active=true. Validate and parse types (Number, Boolean) in the controller before passing options to the service. Do not confuse req.query with req.params: /pastes/5 uses params for id 5; /pastes?sort=desc uses query for sort.