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});
});
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});
});
Other than those on the top, you can use JavaScript to fetch the details from the server. html file
server.js
The best answer i found on the stack-overflow on the said subject, it's not my answer. Found it somewhere for nearly same question...source source of answer
I found the possible way to write.
Server Side -
Client side (main.html) -
What you can utilize is some sort of templating engine like pug (formerly jade). To enable it you should do the following:
npm install --save pug
- to add it to the project and package.json fileapp.set('view engine', 'pug');
- register it as a view engine in express./views
folder and add a simple.pug
file like so:html head title= title body h1= message
note that the spacing is very important!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