How to Pass/Access Node.js Variable to an HTML file/template ?
Last Updated :
26 Jun, 2024
When building web applications with Node.js, it’s often necessary to pass server-side variables to the client-side for rendering dynamic content. This can be achieved using various techniques and templating engines. This article will guide you through different methods to pass and access Node.js variables in HTML files or templates.
Approach
We will be using template engines to access the node js variables in html file. Templating engines enable you to embed Node.js variables directly within your HTML. Popular templating engines include EJS, Pug, and Handlebars. Each has its syntax and way of handling data.
Pug
Pug is a template engine that can be used to inject dynamic data into an HTML page. Its function is to convert the provided data into HTML content and serve it as a static page. Pug's syntax resembles that of traditional HTML but is a lot cleaner and prioritizes indentation and spacing for controlling the flow of logic.Â
Installation Steps
Step 1: We can create a new project with Node Package Manager by using the following command.
npm init
Step 2: Install the required project dependencies i.e express and pug modules using the following command.
npm install express pug --save
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.19.2",
"pug": "^3.0.3"
}
HTML
<!-- index.pug -->
doctype html
html(lang="en")
head
title= 'Node.js Template'
body
h1 My name is #{name}
p I am #{age} years old
JavaScript
// index.js
const express = require('express');
// Initialize App
const app = express();
// Assign route
app.use('/', (req, res, next) => {
res.render('index.pug', { name: 'John Doe', age: 21 });
});
// Start server
app.listen(5000, () => {
console.log('App listening on port 5000');
});
Explanation: Behind the scenes, the above pub code along with the provided data is translated into an HTML page and then served via the Node.js application.
Step to Run Application:Â Run the application using the following command from the root directory of the project
node index.js
Output: Now open your browser and go to http://localhost:5000/, you will see the following output:
Visual Representation of index.pug template
Similar Reads
How to make .js variables accessible to .ejs files ? EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. It is possible to access JS variable in .ejs file.You just need to pass the JS object as second parameter of res.render() method. Let's jump into deep. Project Structure: Final folder structure will be as s
3 min read
How to pass Variable to inline JavaScript using EJS Template Engine? In the EJS templating engine, we can pass variables to inline JS within our HTML templates. We can use the '<%= variable %>' syntax, where we can embed server-side variables directly into the client-side scripts. In this article, we will explore the detailed process to pass a variable to inlin
2 min read
How to pass a Node.js variable to the inside of a Pug script tag ? In Node.js with Pug, we can pass the variables to the Pug template engine using the res.render method. To make the variable available inside the Pug script tag, we include it in the object passed to res.render and then reference it within the Pug file using the syntax "#{variableName}" inside the sc
3 min read
How to Access Variables from Another File using JavaScript? Accessing variables from another file in JavaScript involves exporting the variables from one file and importing them into another. In ES6 modules, use export to share variables and import to access them. In CommonJS (Node.js), use module.exports and require() for the same purpose.Below are the appr
3 min read
How to Render a variable as HTML in EJS ? In the EJS, the rendering of a variable as HTML consists of a specific tage through which we can control how the content is displayed. This tag involves, <% code %> that allows the execution of code without rendering, <%= code %> escapes and prints the code as HTML, while <%- code %
2 min read