Express.js res.render() Function - GeeksforGeeks

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 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
<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 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
<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 
kalrap615
Express.js
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript