In this tutorial we will see how to set content-type header to an express service. The type() method in response object is used to set the type of the response. Consider the below express GET service that returns a JSON response.

Express Set Content Type – Example

var express = require("express");
var app = express();
app.get('/hello',function(req,res){
 	res.type('json');
	res.send('{"name":"Hello"}');
});
app.listen(8080);

Run this service and make a call to it using Postman or similar tools. Check the response headers and you will see the content-type is set to “application/json”.
Passing the value ‘html’ to the type() function will set the content type as ‘text/html’. Similarly, passing ‘png’ will set the type to ‘image/png’.

Express Set Content Type