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
Error: Failed to Execute 'Appendchild' on 'Node': Parameter 1 Is Not of Type 'Node'
How to Change CSS :Root Color Variables in JavaScript
Jquery $("#Radiobutton").Change(...) Not Firing During De-Selection
How to Select Element That Does Not Have Specific Class
Chrome Autofill/Autocomplete No Value for Password
How to Add and Remove Classes in JavaScript Without Jquery
How to Detect Element Being Added/Removed from Dom Element
Phonegap Inappbrowser Display PDF 2.7.0
Trigger Standard HTML5 Validation (Form) Without Using Submit Button
Angularjs Does Not Load Scripts Within Ng-View
Jquery Mobile - $.Mobile.Changepage Not Loading External .Js Files
Maximum Item Size in Indexeddb
Angularjs: Better Way to Watch for Height Change