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 + \"/view
If using Express it's not necessary to use a View Engine at all, use something like this:
<h1>{{ name }} </h1>
This works if you previously set your application to use HTML instead of any View Engine
To pass variables from node.js to html by using the res.render() method.
Example:
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({extend:true}));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/', function(req, res){
res.render('index.html',{email:data.email,password:data.password});
});
I found the possible way to write.
Server Side -
app.get('/main', function(req, res) {
var name = 'hello';
res.render(__dirname + "/views/layouts/main.html", {name:name});
});
Client side (main.html) -
<h1><%= name %></h1>