Open In App
Related Articles

Express.js res.render() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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 accepts 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 the express module: 

You can visit the link to Install the 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 the 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  

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);
});


Create a home.ejs file in the 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: 

Make sure you have installed express and ejs modules using the following command: 

npm install express
npm install ejs

Run the index.js file using the below command: 

node index.js

Output:

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: Filename: index.js  

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);
});


Create a User.ejs file in the 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>


Steps to run the program:

Run the index.js file using the below command: 

node index.js

Output:

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

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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials