res.type()
Sets the Content-Type header from a MIME type or extension.
Syntax
res.type(type) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
type | string | Yes | A MIME type (`'application/json'`) or extension (`'png'`, `'html'`, `'txt'`). |
Returns
Response — The response object, for chaining.
Examples
app.get('/data.csv', (req, res) => {
res.type('csv').send('a,b\n1,2\n');
});
Output
Content-Type: text/csv; charset=utf-8
app.get('/raw', (req, res) => {
res.type('application/octet-stream');
res.send(Buffer.from([1, 2, 3]));
});
Output
Content-Type: application/octet-stream
Notes
Accepts shorthand extensions and looks them up via the `mime-types`
database, adding a `charset` for text types automatically. Cleaner
than `res.set('Content-Type', ...)`. Has no effect after the body
is sent.