Express.js | res.format() Function
Last Updated :
16 Mar, 2023
The res.format() function performs content negotiation on the Accept HTTP header on the request object if it is present. This function checks the Accept HTTP request header and then invokes the corresponding handler depending on the Accept value.
Syntax:
res.format(object)
Installation of the express module:
You can visit the link to Install the express module. You can install this package by using this command.
npm install express
After installing the express module, you can check your express version in the command prompt using the command.
npm version express
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
Project Structure:

Filename: index.js
javascript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', function (req, res) {
res.format({
html: function () {
res.send('<p>Greetings from GeeksforGeeks</p>');
},
text: function () {
res.send('Greetings from GeeksforGeeks');
},
json: function () {
res.send({ message: 'Greetings from GeeksforGeeks' });
}
});
});
app.listen(PORT, function (err) {
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});
In the above example, the output will be { "message": "Greetings from GeeksforGeeks" } when the Accept header field is set to 'application/json' and if the Accept header field is set to 'text/plain', we will get "Greetings from GeeksforGeeks" message in response.
Steps to run the program:
Make sure you have installed the express module using the following command:
npm install express
Run the index.js file using the below command:
node index.js
Output:
Console Output:
Server listening on PORT 3000
Browser Output:
Open your browser and go to http://localhost:3000/, now you can see the following output on your screen.
Greetings from GeeksforGeeks
Similar Reads
Express res.render() Function âThe res.render() function in Express.js is used to render a view template and send the resulting HTML to the client. This allows you to dynamically generate HTML pages by combining your templates with data.Syntax: res.render(view [, locals] [, callback]);Parametersview (required): A string represen
4 min read
How to create helper functions with EJS in Express ? Helper functions in EJS are JavaScrict functions that you can define and use within your templates. These functions can perform various tasks, such as formatting dates, generating dynamic content, or handling repetitive logic. By creating helper functions, you can keep your template code clean and o
2 min read
Node.js util.format() Method The util.format() (Added in v0.5.3) method is an inbuilt application programming interface of the util module which is like printf format string and returns a formatted string using the first argument. The formatted string contains zero or more format specifiers in which the corresponding argument v
5 min read
Node.js MySQL FORMAT() Function FORMAT() function is a built-in function in MySQL that is used to format input numbers in the format of ...**, ***, *** also, round float numbers to a specific number of decimal places. Syntax: FORMAT(number, number_of_decimal_places)Parameters: It takes two parameters as follows: number: The number
2 min read
Express res.json() Function The res.json() function sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using the JSON.stringify() method.Syntax: res.json( [body] )Parameters: The body parameter is the body that is to be sent in the response.Retur
2 min read
Difference between res.send() and res.json() in Express.js? In this article, we will learn about res.send() & res.json(), along with discussing the significant distinction that differentiates between res.send() & res.json(). Let us first understand what is res.send() and res.json() in Express.js? res.send() - The res.send() function is used for sendi
4 min read