Open In App

Express res.render() Function

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

​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]);

Parameters

  • view (required): A string representing the file path of the view template to render.
  • locals (optional): An object containing the data that will be passed to the view template. If a callback function is provided as the third argument, this parameter can be omitted or set to an empty object {}.
  • callback (optional): A function that executes after the view has been rendered. It receives two arguments: err (an error object, if any) and renderedHtml (the rendered HTML string). If this function is not provided, Express will automatically send the rendered HTML to the client.

Returns: This function does not return a value. Instead, it sends the rendered HTML to the client as the response.

Now, let’s discuss the steps to install the express module

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.

HTML
<html>
    <head>
        <title>res.render() Demo</title>
    </head>
    <body>
        <h2>Welcome to GeeksforGeeks</h2>
    </body>
</html>
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);
});

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.

HTML
<html>
    <head>
        <title>res.render() Demo</title>
    </head>
    <body>
        <h2>Render Function Demo</h2>
    </body>
</html>
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);
});

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


Next Article

Similar Reads