Passing a Variable from Node.Js to HTML

Passing a variable from node.js to html

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

<script>var name = "<%= name %>";</script>
console.log(name);

passing node js variable to html using ajax

try changing arguments in python results callback:

PythonShell.PythonShell.run('dbPrac.py', options, function(err, pythonRes) {
if(err) throw err;
console.log('pythonRes[0]: ', pythonRes[0]);
//res.render(__dirname + '/data.html');
res.send(pythonRes[0]);

Take HTML form and pass values to variables in node.js function

This following Node.js code will solve your question.
You need a route to return your main html file and a route that gets post data and returns a result based on this data.

var express = require('express')
var http = require('http');
var math = require('mathjs');
var bodyParser = require('body-parser');

var app = express()

// use body parser to easy fetch post body
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())

// route to '/' to return the html file
app.get('/', function (req, res, next) {
res.sendfile('index.html');
});

//route that receives the post body and returns your computation
app.post('/solve', function (req, res, next) {
pleaseSolve(req.body, res);
});

app.listen(8080);

function pleaseSolve(parms, res) {
//get the parameters based on input name attribute from the html
//and parse strings to numbers
var m = +parms.param1;
var o = +parms.param2;
var p = +parms.param3;

var comp = math.chain(m)
.add(m)
.divide(p)
.multiply(o)
.done();

res.writeHead(200, { 'Content-Type': 'text/html' });
res.end("The answer is " + comp);
}


Related Topics



Leave a reply



Submit