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();

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