Express.js | res.format() Function Last Updated : 16 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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 Comment More infoAdvertise with us Next Article Express.js | res.format() Function gouravhammad Follow Improve Article Tags : JavaScript Web Technologies Node.js Express.js Similar Reads 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 Express.js res.get() Function The res.get() function returns the HTTP response header specified by the field. The match is case-insensitive. Syntax: res.get( field ) Parameter: The field parameter describes the name of the field. Return Value: It returns an Object. Installation of the express module: You can visit the link to 2 min read Express.js res.set() Function The res.set() function is used to set the response HTTP header field to value. To set multiple fields at once, pass an object as the parameter.Syntax: res.set(field [, value])Parameters: The field parameter is the name of the field and the value parameter is the value assigned to the field parameter 2 min read Express.js res.type() Function 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 mod 2 min read Express.js req.is() Function The req.is() function returns the matching content-type if the incoming requestâs 'Content-Type' HTTP header field matches with the MIME type that has been specified by the type parameter and it returns null if the request has no body otherwise it returns false. Syntax: req.is( type ) Parameter: The 2 min read Express.js res.vary() Function The res.vary() function is used to add the field to the Vary response header, if it is not there already. The Vary header indicates which headers itâs basically used for content negotiation. Syntax: res.vary( field ) Parameter: The field parameter describes the name of the field. Return Value: It r 2 min read Express.js res.jsonp() Function The res.jsonp() function is used to send a JSON response with JSONP support and this function is similar to the res.json() function except that it opts-in to the support of JSONP callback. Syntax: res.jsonp( [body] ) Parameter: The body parameter describes the body type which can be sent in respons 2 min read Express.js res.links() Function The res.links() function is used to join the links provided as properties of the parameter to populate the responseâs Link HTTP header field. Syntax: res.links( links ) Parameter: The link parameter describes the name of the link to be joined. Return Value: It returns an Object. Installation of the 2 min read Express.js res.append() Function The res.append() function appends the specified value to the HTTP response header field and if the header is not already set then it creates the header with the specified value. Syntax: res.append(field [, value])Parameter: The field parameter describes the name of the field that need to be appended 2 min read Express.js req.get() Function The req.get() function returns the specified HTTP request header field which is a case-insensitive match and the Referrer and Referrer fields are interchangeable. Syntax:req.get( field )Parameter: The field parameter specifies the HTTP request header field. Return Value: String. Installation of the 2 min read Like