How to Setup View Engine in Node.js ? Last Updated : 27 Apr, 2020 Comments Improve Suggest changes 10 Likes Like Report View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with plain javascript. Installation of ejs module: You can visit the link Install ejs module. You can install this package by using the following command. npm install ejs After installing multer you can check your ejs version in command prompt using the command. npm version ejs After that, you can just create a folder and add a file for example app.js, To run this file you need to run the following command. node app.js To setup view engine, you need the write this middleware in your index.js as follow: app.set('views', path.join(__dirname, 'views')) app.set('view engine', 'ejs') where path is the global object and __dirname holds current directory address. Views is the folder where our all web pages will be kept. Now create a EJS file like Demo.ejs and put this file in views folder. Filename: Demo.ejs html <!DOCTYPE html> <html> <head> <title>View Engine Demo</title> </head> <body> <!- For printing variable these tags are used: <%= %> --> <h1> <%= title %> </h1> <!- For business logic these tags are used: <% %> --> <% if(true){ %> <h4>Greetings from geeksforgeeks</h4> <% } %> </body> </html> Filename: app.js javascript const express = require('express') const path = require('path') const app = express() // View Engine Setup app.set('views', path.join(__dirname, 'views')) app.set('view engine', 'ejs') app.get('/', function(req, res){ // Rendering our web page i.e. Demo.ejs // and passing title variable through it res.render('Demo', { title: 'View Engine Demo' }) }) app.listen(8080, function(error){ if(error) throw error console.log("Server created Successfully") }) Steps to run the program: The project structure will look like this: Make sure you have 'view engine' like I have used "ejs" and also install express using the following commands: npm install ejs npm install express Run app.js file using below command: node app.js Open browser and type this URL: http://localhost:8080/ Then you will see the Demo.ejs page as shown below: So this is how you can setup a view engine in node js. There are many other engines exist like Handlebars, Mustache, etc. Create Quiz Comment G gouravhammad Follow 10 Improve G gouravhammad Follow 10 Improve Article Tags : Node.js Node.js-Misc Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like