Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a variable from node.js to html

I am trying to pass a variable from node.js to my HTML file.

app.get('/main', function(req, res) {   var name = 'hello';   res.render(__dirname + "/views/layouts/main.html", {name:name}); }); 
like image 512
vic-3 Avatar asked Jun 23 '16 12:06

vic-3


People also ask

Can you use a variable from JavaScript in html?

You cannot use js variables inside html. To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

Can you call node js function from html?

If you need to call a NodeJS function (assuming it has to interact with the other server side code such as databases etc) then you can send a request, either via AJAX or standard HTTP, to the a route on the server and call that function within the route.

How to pass array of values from Node JS to HTML?

All the codes means that when you access to the home page, node.js will render file “index.ejs” in “views” folder. And the “title” variable will be pass to the html, the title variable’s value will be replace to the “title” in html. Now, you want to pass the array into file html, you can do the following:

How to view data from Node JS to HTML?

There are two approaches here you can use to view data from node ( server side ) to html: 1- You could create a file in node which return data in json, then from JQuery you could do an ajax call this page and replace parts of the HTML with it. Sample code in node js:

How do I use body parser in Node JS?

Just type the command $npm i data-parser and the data-parser package downloads to your machine. Next, to let our code know we'd like it to use body-parser, we create the following variable at the top of the index.js file: const bodyParser = require ("body-parser");.

How to get the home page of a website using Node JS?

You can see something like: The first parameter of the get function, it is “/”, means the home page of website. All the codes means that when you access to the home page, node.js will render file “index.ejs” in “views” folder. And the “title” variable will be pass to the html, the title variable’s value will be replace to the “title” in html.


2 Answers

I figured it out I was able to pass a variable like this

<script>var name = "<%= name %>";</script> console.log(name); 
like image 175
vic-3 Avatar answered Sep 21 '22 05:09

vic-3


What you can utilize is some sort of templating engine like pug (formerly jade). To enable it you should do the following:

  1. npm install --save pug - to add it to the project and package.json file
  2. app.set('view engine', 'pug'); - register it as a view engine in express
  3. create a ./views folder and add a simple .pug file like so:
html   head     title= title   body     h1= message 

note that the spacing is very important!

  1. create a route that returns the processed html:
app.get('/', function (req, res) {   res.render('index', { title: 'Hey', message: 'Hello there!'}); }); 

This will render an index.html page with the variables passed in node.js changed to the values you have provided. This has been taken directly from the expressjs templating engine page: http://expressjs.com/en/guide/using-template-engines.html

For more info on pug you can also check: https://github.com/pugjs/pug

like image 45
Vasil Dininski Avatar answered Sep 19 '22 05:09

Vasil Dininski