Handling Requests

1. Accessing Request Object

Inherits FromDetail
http.IncomingMessageNode.js base class — stream + headers
Express extensionsparams, query, body, ip, route, etc.

2. Reading Request Parameters

Example: req.params

app.get("/orgs/:orgId/users/:userId", (req, res) => {
  res.json({ org: req.params.orgId, user: req.params.userId });
});
PropertySource
req.paramsURL named segments
req.params[0]Wildcard / unnamed regex group

3. Reading Query Strings

SourceProperty
Parsed queryreq.query
Raw query stringreq.url.split("?")[1]

4. Accessing Request Body

Example: JSON body

app.use(express.json());
app.post("/users", (req, res) => {
  const { name, email } = req.body;
  res.status(201).json({ name, email });
});
Note: req.body is undefined until a body parser middleware runs.

5. Reading Request Headers

APIDescription
req.headersAll headers (lowercase keys)
req.get(name) / req.header(name)Case-insensitive lookup
req.headers["x-custom"]Direct access

Example: Bearer token

const auth = req.get("Authorization") || "";
const token = auth.startsWith("Bearer ") ? auth.slice(7) : null;

6. Getting Request Method

PropertyValue
req.method"GET", "POST", "PUT", "PATCH", "DELETE", etc. (uppercase)
req.xhrtrue if X-Requested-With: XMLHttpRequest

7. Getting Request URL

PropertyExample for GET /api/users?x=1 mounted at /api
req.originalUrl/api/users?x=1 (full path with query)
req.baseUrl/api (router mount path)
req.url/users?x=1 (after baseUrl strip)
req.path/users (no query)

8. Getting Request Path

Example: Path-based logic

app.use((req, res, next) => {
  if (req.path.startsWith("/admin")) return requireAdmin(req, res, next);
  next();
});

9. Checking Request Protocol

PropertyValue
req.protocol"http" or "https" (honors X-Forwarded-Proto if trust proxy)
req.securereq.protocol === "https"

Example: Force HTTPS redirect

app.use((req, res, next) => {
  if (req.secure || req.hostname === "localhost") return next();
  res.redirect(301, `https://${req.hostname}${req.originalUrl}`);
});

10. Getting Request Hostname

PropertySource
req.hostnameHost header (or X-Forwarded-Host with trust proxy)
req.subdomainsArray (tenant1.api.example.com["tenant1","api"])

11. Accessing Request IP

PropertyBehavior
req.ipClient IP (uses X-Forwarded-For if trust proxy set)
req.ipsArray of all proxy hops
req.socket.remoteAddressRaw TCP peer (always direct)
Warning: Without trust proxy, req.ip returns the load balancer's IP, not the real client.

12. Reading Cookies

import cookieParser from "cookie-parser";
app.use(cookieParser(env.COOKIE_SECRET));

app.get("/", (req, res) => {
  const session = req.cookies.session;
  const signed = req.signedCookies.userId;
  res.json({ session, signed });
});
PropertyDescription
req.cookiesParsed cookies (object)
req.signedCookiesVerified signed cookies

13. Checking Content Type

Example: req.is()

if (req.is("json"))            handleJson();
else if (req.is("urlencoded")) handleForm();
else if (req.is("multipart/*")) handleUpload();
else                            res.sendStatus(415);
PatternMatches
"json"application/json, application/*+json
"html"text/html
"text/*"Any text subtype
"image/png"Exact