Read route parameters
A route needs values captured from a URL pattern such as /users/:id.
Express.js · Interface
The Express req object represents an incoming HTTP request. It exposes route parameters, query data, body data, headers, request metadata, and helpers for inspecting or negotiating the request.
Express supplies the incoming request to route and middleware callbacks as req. The object extends Node’s request object and exposes request metadata, parsed route and query values, body data when body-parsing middleware is used, headers, and helper methods such as get() and accepts().
(req, res, next)| Name | Type | Description |
|---|---|---|
req | Request | The incoming HTTP request supplied to a route or middleware callback. |
An Express request object containing request properties and methods.
req to inspect data and metadata associated with the incoming HTTP request.req.params maps each named route placeholder to the value captured from the URL.req.body represents submitted body fields after body-parsing middleware has populated it.req.method reports the request method, including values such as GET, POST, and PUT.req.app lets middleware access the Express application instance handling the request.req.body is unavailable until suitable body-parsing middleware, such as express.json() or express.urlencoded(), processes the request.req.files automatically; use multipart-handling middleware when needed.const express = require('express');
const app = express();
app.get('/users/:name', (req, res) => {
res.json({
name: req.params.name,
query: req.query,
method: req.method,
userAgent: req.get('User-Agent')
});
});
app.listen(3000);Run the server, then request /users/Ada?active=true to see route parameters, query data, the HTTP method, and a header.
A route needs values captured from a URL pattern such as /users/:id.
An endpoint filters or paginates results using values after ? in the URL.
Middleware needs to inspect authorization, content type, or another incoming header.
A handler selects a response representation based on the client’s Accept header.
req.params, req.query, and req.body differ?req.params contains values captured from named route segments, such as :name. req.query contains parsed query-string values, while req.body contains submitted body data only after body parsing.
Follow-up: How would you read a request header? Use req.get(field), which performs a case-insensitive lookup; the documentation also treats Referrer and Referer as interchangeable.
req.method identifies the HTTP method, such as GET or POST. req.protocol identifies http or https, and req.get() retrieves a request-header value.
Follow-up: How can content negotiation be checked? Call req.accepts(types) to find the best acceptable type, or handle an undefined result as a 406 response.
Express’s request object is enhanced from Node’s own request object, so it retains Node’s built-in fields and methods while adding Express-specific properties and helpers. It is conventionally named req, but the callback parameter can use another name.
Follow-up: What does req.app provide? It references the Express application instance that is using the middleware.
req.appreq.bodyreq.get(field)req.methodreq.paramsreq.protocolreq.queryreq.res