Express.js res.type() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Response

Definition

res.type(type) sets the response Content-Type header. If type contains /, Express uses that string as-is. Otherwise it treats type as a file extension and looks up the MIME type with the mime-types package.

It is aliased as res.contentType(). Call it before you send the body with res.send(), res.end(), or similar. Methods like res.json() may set Content-Type for you.

Syntax

Call res.type in your Express.js app. Start with this line.

Express
res.type(type)
res.contentType(type)   // alias
CallWhat it does
res.type(type)Primary signature.
res.contentType(type) // aliasRelated call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

res.type(type)
res.contentType(type)   // alias

Parameters

ParameterDescription
typeRequired. MIME type (contains /) or file extension looked up to a MIME type.
'.html' or 'html'Required. text/html
'json'Required. application/json
'application/json'Required. application/json (exact)
'png'Required. image/png

Return Value

res.type() (alias res.contentType) returns res for chaining. It sets Content-Type only — it does not send a body.

Express
res.type('json').send({ ok: true });
res.type('text/plain').send('hi');

Express.js Examples

Example 1 — Set Type from an Extension

html looks up text/html, then send a small page.

Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.type('html');
  res.send('<h1>Hello</h1>');
});

app.listen(3000);

How It Works

  1. Pass an extension. 'html' has no /, so Express looks it up.
  2. Header is set. Content-Type becomes text/html (charset may be added on send).
  3. Send the body. The browser treats the reply as HTML.
  4. Same as .html. res.type('.html') resolves the same way.

Example 2 — Exact MIME String

A value with / is written exactly — useful when you send text that is JSON-shaped without res.json().

Express
const express = require('express');
const app = express();

app.get('/raw-json', (req, res) => {
  res.type('application/json');
  res.send('{"ok":true}');
});

app.listen(3000);

How It Works

  1. Exact MIME. Because of /, no extension lookup runs.
  2. Header matches the string. Clients expect JSON.
  3. Body is a string. You are responsible for valid JSON text.
  4. Simpler path. For objects, prefer res.json({ ok: true }) instead.

Example 3 — Image Type Before Binary Send

Set image/png from the png extension, then end with binary data (demo Buffer).

Express
const express = require('express');
const app = express();

app.get('/pixel.png', (req, res) => {
  // Tiny 1x1 PNG bytes (illustrative)
  const png = Buffer.from(
    'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
    'base64'
  );
  res.type('png').send(png);
});

app.listen(3000);

How It Works

  1. Extension lookup. 'png' maps to image/png.
  2. Chain send. type returns res, so .send(png) follows.
  3. Buffer body. Binary is sent with the type you chose.
  4. Alias works too. res.contentType('png') is the same call.

Runtime Support

res.type() is an Express.js response method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers only receive the Content-Type on the HTTP response.

✓ Server · Express 4 & 5

res.type()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
res.type() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It sets the Content-Type response header. If the argument contains a slash, it is used as-is; otherwise Express looks up a MIME type from a file extension via the mime-types package.
No. res.contentType() is an alias of res.type() and behaves the same way.
No. It only sets the header. Finish with res.send(), res.end(), or another response method.
Use res.set when you need an exact header string (including unusual parameters). Prefer res.type for short extensions like json or html.
No. It runs on your Node.js server. Browsers only receive the Content-Type on the HTTP response.

Next: res.vary()

Add a field to the Vary response header for caching.

res.vary() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful