Express.js res.type() Function
Last Updated :
16 Mar, 2023
The res.type() function is used to set the Content-Type HTTP header to the MIME type determined by the mime.lookup() function for the specified type.
Syntax:
res.type( type )
Parameters: The type parameter describes the MIME type.
Return Value: It returns an Object.
Installation of the express module:
1. You can visit the link to Install the express module. You can install this package by using this command.
npm install express
2. After installing the express module, you can check your express version in the command prompt using the command.
npm version express
3. After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.
node index.js
Example 1: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
// Without middleware
app.get('/', function (req, res) {
res.type('.png').send();
// image/png
console.log(res.get('Content-type'));
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Steps to run the program:
- The project structure will look like this:

1. Make sure you have installed the express module using the following command:
npm install express
2. Run the index.js file using the below command:
node index.js
Output:
Server listening on PORT 3000
Now open the browser and go to http://localhost:3000/, now check your console and you will see the following output:
Server listening on PORT 3000
image/png
Example 2: Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
// With middleware
app.use('/', function (req, res, next) {
res.type('.png').send();
next();
})
app.get('/', function (req, res) {
console.log("Content-Type: ",
res.get('Content-type'));
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
Run the index.js file using the below command:
node index.js
Now open the browser and go to http://localhost:3000/, now check your console and you will see the following output:
Server listening on PORT 3000
Content-Type: image/png
Reference: https://expressjs.com/en/5x/api.html#res.type
Similar Reads
Express res.status() Function The res.status() function sets the HTTP status for the response. It is a chainable alias of Nodeâs response.statusCode. Syntax:res.status( code )Parameter: This function accepts a single parameter code that holds the HTTP status code. Return Value: It returns an Object. Steps to create the Express A
2 min read
Express res.end() Function The res.end() function concludes the response process and is derived from the HTTP.ServerResponse's response.end() method in the Node core. It is employed to promptly conclude the response without including any data.Syntax: res.end([data] [, encoding])Parameters: The default encoding is 'utf8' and t
2 min read
Express app.get() Request Function The app.get() function is used to define routes on your server that handle HTTP GET requests. A GET request is typically used when the client asks the server to send back some information, like retrieving a webpage or data from a database.Itâs an essential part of building websites and APIs, as it a
6 min read
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Express.js express.text() Function The express.text() function is a built-in middleware in Express.js that parses incoming HTTP request bodies with a text/plain content type. It allows you to easily handle raw text data sent in the body of a request, making it suitable for handling non-JSON, non-URL-encoded, or non-multipart data.The
5 min read
Express res.cookie() Function The res.cookie() function is used to set a cookie in the client's browser. It allows you to assign a cookie by providing a name and a value. The value can be a simple string or an object, which will be automatically converted to JSON.Syntax:res.cookie(name, value [, options])name: The name of the co
2 min read