I just wanna to pass variables from a HTML page to a node js and perform some calculations on the data and get it back to HTML using ejs After installing ejs :
npm install ejs
I'm trying to pass this variable temp with value 50 "HTML Page":
<html>
<head>
</head>
<body>
My temperature: <%= temp=50 %>
Temp + 10 : <%= total %>
</body>
</html>
and my nodejs server.js:
var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
//since we are in a request handler function
//we're using readFile instead of readFileSync
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var temp; //here you assign temp variable with needed value
var total = temp+10;
var renderedHtml = ejs.render(content, {temp: temp, total:total}); //get redered HTML code
res.end(renderedHtml);
});
}).listen(8080);
Any help would be appreciated Thanks in advance.