Skip to content
Related Articles

Related Articles

Improve Article

Express.js res.render() Function

  • Last Updated : 19 Aug, 2021

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 returns an Object.

Installation of express module: 



1. You can visit the link to Install express module. You can install this package by using this command. 

npm install express

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

npm version express

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

Javascript




var express = require('express');
var app = express();
var 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);
});

Create a home.ejs file in views folder with the following code: 
Filename: home.ejs 

HTML




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

Steps to run the program: 



1. Make sure you have installed express and ejs module using the following command: 

npm install express
npm install ejs

2. Run index.js file using below command: 

node index.js

Output: 

Server listening on PORT 3000

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

Javascript




var express = require('express');
var app = express();
var 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);
});

Create a User.ejs file in views folder with the following code: 
Filename: User.ejs 

HTML




<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

Reference: https://expressjs.com/en/5x/api.html#res.render
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :