Express.js res.render() Function
The res.render() function is used to render a view and sends the rendered HTML string to the client.
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.
Example 1: Filename: index.js
Create a home.ejs file in views folder with the following code:
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:
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
Create a User.ejs file in views folder with the following code:
Run index.js file using below command:
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