Open In App
Related Articles

Express res.render() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The res.render() function in Express is used to render a view and sends the rendered HTML string to the client. 

Syntax: 

res.render(view [, locals] [, callback])

Parameters: This function accepts two parameters as mentioned above and described below:  

  • Locals: It is an object whose properties define local variables for the view.
  • Callback It is a callback function.

Returns: It returns an Object.

Steps to Install the express module:

Step 1: Installing the required modules

npm install express ejs

Step 2: After installing the express module, you can check your express version in the command prompt using the command.

npm version express

Project Structure:

NodeProj

Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2",
}

Example 1: Below is the code of res.render() Function implementation.

Javascript

const express = require('express');
const app = express();
const PORT = 3000;
 
// View engine setup
app.set('view engine', 'ejs');
 
// Without middleware
app.get('/user', function (req, res) {
 
    // Rendering home.ejs page
    res.render('home');
})
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

                    

HTML

<html>
    <head>
        <title>res.render() Demo</title>
    </head>
    <body>
        <h2>Welcome to GeeksforGeeks</h2>
    </body>
</html>

                    

Steps to run the program: 

Run the index.js file using the below command: 

node index.js

Console Output: 

Server listening on PORT 3000

Browser Output:

Now open the browser and go to http://localhost:3000/user, you can see the following output on your screen: 

Welcome to GeeksforGeeks

Example 2: Below is the code of res.render() Function implementation.

Javascript

const express = require('express');
const app = express();
const PORT = 3000;
 
// View engine setup
app.set('view engine', 'ejs');
 
// With middleware
app.use('/', function (req, res, next) {
    res.render('User')
    next();
});
 
app.get('/', function (req, res) {
    console.log("Render Working")
    res.send();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

                    

HTML

<html>
    <head>
        <title>res.render() Demo</title>
    </head>
    <body>
        <h2>Render Function Demo</h2>
    </body>
</html>

                    

Steps to run the program:

Run the index.js file using the below command: 

node index.js

Console Output: After running the above command, you will see the following output on your console screen:  

Server listening on PORT 3000
Render Working

Browser Output: Now open the browser and go to http://localhost:3000, you can see the following output on your screen:  

Render Function Demo


Last Updated : 01 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads