I just want to transfer the variables from the HTML page to node js and do some data calculations and return them to HTML using ejs After installing ejs:
npm install ejs
I am trying to pass this temp variable with a value of 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'});
fs.readFile('index.html', 'utf-8', function(err, content) {
if (err) {
res.end('error occurred');
return;
}
var temp;
var total = temp+10;
var renderedHtml = ejs.render(content, {temp: temp, total:total});
res.end(renderedHtml);
});
}).listen(8080);
Any help would be appreciated Thanks in advance.
source
share