SyntaxStudy
Sign Up
Express.js Beginner 1 min read

The Request Object

Express augments Node's native `http.IncomingMessage` with additional properties and methods, exposing them on the `req` object. The most frequently used properties are `req.params` (URL segments), `req.query` (parsed query string), `req.body` (parsed request body, populated by body-parser middleware), `req.headers`, and `req.ip`. `req.path` returns the path without the query string; `req.originalUrl` includes the query string and is unmodified by router mounting. `req.baseUrl` gives the path prefix at which a Router was mounted, useful for building absolute URLs inside nested routers. `req.get(header)` retrieves a request header case-insensitively. `req.is(type)` checks the `Content-Type` header. `req.cookies` and `req.signedCookies` are populated by the `cookie-parser` middleware. Together, these properties give complete access to all aspects of the incoming HTTP message.
Example
const express = require('express');
const app     = express();
app.use(express.json());
app.use(require('cookie-parser')());

app.post('/inspect/:version/data', (req, res) => {
    console.log('params  :', req.params);   // { version: 'v1' }
    console.log('query   :', req.query);    // { debug: 'true' }
    console.log('body    :', req.body);     // parsed JSON object
    console.log('ip      :', req.ip);
    console.log('method  :', req.method);   // POST
    console.log('path    :', req.path);     // /inspect/v1/data
    console.log('baseUrl :', req.baseUrl);  // '' (top-level app)
    console.log('headers :', req.headers);

    // Safe header access
    const contentType  = req.get('Content-Type');
    const acceptsJson  = req.accepts('application/json');
    const isJson       = req.is('application/json');

    res.json({
        version:     req.params.version,
        debug:       req.query.debug === 'true',
        contentType,
        acceptsJson,
        isJson,
    });
});

app.listen(3000);