Express res.json() Function Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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.Return Value: It returns an Object.Steps to Install the Express Module:Step 1: You can install this package by using this command.npm install expressStep 2: After installing the express module, you can check your express version in the command prompt using the command.npm version expressProject Structure:Project StructureThe updated dependencies in package.json file will look like:"dependencies": { "express": "^4.18.2",}Example 1: Below is the code of res.json() Function implementation. javascript const express = require('express'); const app = express(); const PORT = 3000; // Without middleware app.get('/', function (req, res) { res.json({ user: 'geek' }); }); app.listen(PORT, function (err) { if (err) console.log(err); console.log("Server listening on PORT", PORT); }); Steps to run the program:Run the index.js file using the below command: node index.jsConsole Output: Server listening on PORT 3000Browser Output: Now open the browser and go to http://localhost:3000/, now on your screen you will see the following output: {"user":"geek"}Example 2: Below is the code of res.json() Function implementation. javascript const express = require('express'); const app = express(); const PORT = 3000; // With middleware app.use('/', function (req, res, next) { res.json({ title: "GeeksforGeeks" }) next(); }) app.get('/', function (req, res) { console.log("User Page") res.end(); }); app.listen(PORT, function (err) { if (err) console.log(err); console.log("Server listening on PORT", PORT); }); Steps to run the program: Run the index.js file using the below command: node index.jsBrowser Output: Now open a browser and go to http://localhost:3000/, now on your screen you will see the following output: {"title":"GeeksforGeeks"}Console Output: And you will see the following output on your console: User Page Comment More infoAdvertise with us Next Article Express res.json() Function gouravhammad Follow Improve Article Tags : JavaScript Web Technologies Node.js Express.js Similar Reads 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.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.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.location() Function The res.location() function is used to set the response Location HTTP header to the path parameter which is being specified. Basically, it is used to set the response header. It doesn't end the response, after using it you can write a response body if you want to write. Syntax: res.location( path ) 2 min read 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 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 Express.js | res.format() Function 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 expre 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 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.js req.param() Function The req.param() function basically returns the value of the param name when present. When parameters are passed, then it can be accessed using this function. Syntax: req.param(name [, defaultValue]) Parameter: The name parameter is the name of the parameter. Return Value: String. Installation of th 2 min read Like