The res.render() function is used to render a view and sends the rendered HTML string to the client.
Syntax:
res.render(view [, locals] [, callback])
Parameters: This function accept two parameters as mentioned above and described below:
- Locals: It is basically an object whose properties define local variables for the view.
- Callback It is a callback function.
Returns: It retuns an Object.
Installation of express module:
- You can visit the link to Install 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 command prompt using the command.
npm version express
- After that, you can 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
Example 1: Filename: index.js
var express = require('express');var app = express();var PORT = 3000; // View engine setupapp.set('view engine', 'ejs'); // Without middlewareapp.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);}); |
Create a home.ejs file in views folder with the following code:
Filename: home.ejs
<html><head> <title>res.render() Demo</title></head><body> <h2>Welcome to GeeksforGeeks</h2></body></html> |
Steps to run the program:
- Make sure you have installed express and ejs module using the following command:
npm install express npm install ejs
- Run index.js file using below command:
node index.js
Output:
Server listening on PORT 3000
- Now open browser and go to http://localhost:3000/user, you can see the following output on your screen:
Welcome to GeeksforGeeks
Example 2: Filename: index.js
var express = require('express');var app = express();var PORT = 3000; // View engine setupapp.set('view engine', 'ejs'); // With middlewareapp.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);}); |
Create a User.ejs file in views folder with the following code:
Filename: User.ejs
<html><head> <title>res.render() Demo</title></head><body> <h2>Render Function Demo</h2></body></html> |
Run index.js file using below command:
node index.js
After running above command, you will see the following output on your console screen:
Server listening on PORT 3000 Render Working
Now open browser and go to http://localhost:3000, you can see the following output on your screen:
Render Function Demo


