Requests and Routing in Express.js

Requests and Routing is the second chapter of the free Express.js tutorial. It covers the app object, everything a request carries, and every way to route it to the right code.

What each lesson covers:

  • Application, Request and Response: The app object and its settings, app.locals, sub-applications, everything req holds, every way res can answer, the one-response rule, and caching with ETags and 304 answers.
  • Routing and Routers: Route methods, parameters, query strings, the Express 5 path syntax, route order, handler chains, app.route, router files, nested routers, mergeParams and router.param.

The first lesson covers the three objects you use in every handler. You configure the application with settings and app.locals, and mount a sub-application. You read the URL, query, headers and connection details from req, and answer with res: status codes, headers, JSON and caching, while keeping to the rule of one response per request. The second lesson builds routes: methods, paths with Express 5 syntax, query strings, route order, chained handlers with app.route(), and express.Router with nested routers, mergeParams and router.param.

Every lab runs a real Express 5 server in your browser, with a request panel to call it like Postman or curl.

Lessons in this section

  1. Application, Request and Response

    The app object and its settings, app.locals and sub-apps, everything req holds, every way res can answer, the one-response rule, and caching with ETags, 304 answers and range requests.

    • Create and configure the app object
    • Read params, query, body and headers from req
    • Send text, JSON and status-only responses
    • Avoid double responses with return
  2. Routing and Routers

    Route methods, parameters, query strings, the Express 5 path syntax, route order, handler chains, app.route, router files, router middleware, nested routers, mergeParams and router.param.

    • Match methods and paths
    • Read route params and query strings
    • Use the Express 5 path syntax
    • Order routes correctly