Content type and length
Set content metadata before returning a body.
Express.js · Module
Sets one or more HTTP response headers. Use a field/value pair for one header or an object for several headers, and chain the returned response object.
Use res.set() in a route handler to assign HTTP response headers. Pass a field and value for one header, or pass an object containing several header-value pairs; the method returns the response object so calls can be chained.
res.set(field [, val])| Name | Type | Description |
|---|---|---|
field | string | object | The response header field name, or an object mapping header names to values. |
val | string | The value for the selected response header field. |
The response object.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.set('Cache-Control', 'no-store');
res.set({
'X-Content-Type-Options': 'nosniff',
'Content-Type': 'text/plain; charset=utf-8'
});
res.send('Hello');
});
app.listen(3000);Sets individual and multiple response headers before sending a plain-text response.
Set content metadata before returning a body.
Apply browser or intermediary caching directives.
Add application-specific metadata to an outgoing response.
res.set() assigns headers on the outgoing HTTP response. It accepts either one header name with its value or an object containing multiple headers.
Follow-up: What happens if it follows res.append()? The set operation resets the previously appended header value.
The single-header form is res.set(field, [val]), while the object form is res.set(field). The object form is useful when several headers must be configured together.
Follow-up: How can you set both cache policy and a custom header? Pass an object with both field-value pairs to res.set().
Headers should be configured before the response is completed, such as before calling res.send(). The method belongs to Express's response object and is used inside a request handler.
Follow-up: What should be checked when a header appears missing? Verify that the header was set before the response was sent and that a later operation did not replace it.
res.append()res.get()res.send()res.type()