Inspect response metadata
Middleware or a route needs to inspect an outgoing header before completing the response.
Express.js · Module
Reads a response header from Express's res object by name. Header-name matching is case-insensitive, and the method returns the selected header value.
Call res.get() inside a handler to read a response header previously available on the outgoing response. Pass the header name as a string; matching ignores letter case, and the method returns the selected header value.
res.get(field)| Name | Type | Description |
|---|---|---|
field | String | Name of the response header to read; matching is case-insensitive. |
String: the HTTP response header specified by field.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send(`Stored type: ${res.get('Content-Type')}`);
});
app.listen(3000);Starts an Express server, sets a response header, reads it case-insensitively with res.get(), and includes the value in the response.
Middleware or a route needs to inspect an outgoing header before completing the response.
Application diagnostics need to verify the value assigned to a response header.
Code should read a header without depending on its capitalization.
res.get(field) reads the HTTP response header named by field and returns its value as a string. The lookup is case-insensitive, so different capitalization of the same header name works.
Follow-up: What happens if the header has not been set? The page does not specify a missing-header return behavior, so avoid assuming a particular value without checking the relevant runtime behavior.
The argument is the response-header name, represented as a String. The method reads from the Express response object associated with the current request handler.
Follow-up: Why is the response object available in a route handler? Express passes it as the handler's res parameter, whose actual variable name can differ.
A typical use is to inspect a response header after setting it, for example reading Content-Type before composing or logging a response. The returned value is the HTTP response header specified by the field argument.
Follow-up: How would you obtain a request header instead? Use the request object's header-reading API rather than res.get(), because res.get() targets the outgoing response.
res.set()res.header()res.append()res.type()res.send()