Open In App

How to run pug View Engine using Express?

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pug or Jade is the template engine for NodeJS and browsers that are used for the process of developing dynamic HTML content. This template engine uses the indentation-based syntax like Python to generate the HTML markup, which makes it simpler to write and also to maintain the code. If we want to run the Pug, we need to install it using the npm (Node Package Manager) and then use the command to integrate it into the NodeJS application to render the Pug template into HTML. In this article, we will go through the detailed step-by-step process to run Pug.

Steps to create Pug Application:

Step 1: Create a backend server using the following command in your project folder.

npm init -y

Step 2: Install the necessary package in your server using the following command.

npm i express pug

Folder Structure:

PS

The updated dependencies in package.json file will look like:

"dependencies": {  
"express": "^4.18.2",
"pug": "^3.0.2"
}

HTML




// views/index.pug
doctype html
html
  head
    title GeeksforGeeks Pug Run
  body
    h1 Welcome to GeeksforGeeks
    p Hello GeeksforGeeks. This is online Coding Learning Platform.


Javascript




// server.js
const express = require('express');
const app = express();
const port = 3000;
app.set('view engine', 'pug');
app.get('/', (req, res) => {
    res.render('index');
});
app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});


To run the pug application, we need to start the server by using the below command.

node app.js

Output:

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads