Express res.type() Method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to set response mime type using res.type().
  • How extension-based mime mapping works.
  • How to combine type setting with send/json methods.
  • How to avoid wrong content-type responses.

Syntax

javascript
res.type(type)
1

Send plain text with explicit mime

javascript
app.get('/plain', function (req, res) {
  res.type('text/plain');
  res.send('Hello from Express');
});
2

Use extension shortcut

javascript
app.get('/csv', function (req, res) {
  res.type('csv');
  res.send('id,name\n1,Mari');
});
3

Set HTML type before response

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

⚠️ Common pitfalls

  • Set content type before final send to avoid header timing issues.
  • Do not assume type is correct when sending uncommon formats without explicitly setting it.
  • Use res.json() for JSON APIs when possible for clearer intent.

❓ FAQ

It sets the Content-Type header for the response.
Yes, extensions like 'json', 'html', or 'png' are supported and mapped to mime types.
Yes, you can pass values like 'application/json' directly.
Use it when you want explicit content-type control before send/end methods.
No, it only sets the header. You still need to send or end the response.
Did you know?

res.type() sets the Content-Type header and accepts extensions like json, html, or full mime values.

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.

4 people found this page helpful