VOOZH about

URL: https://www.geeksforgeeks.org/web-tech/express-js-res-render-function/

⇱ Express res.render() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Express res.render() Function

Last Updated : 4 Apr, 2025

​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.

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.

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
Comment