My aim here is to create a HTML form that when you submit 2 numbers they are added together server side in node.js and the answer is displayed in a readonly input field in the same HTML page. At present, my function outputs the answer in a separate page- I don't want to redirect the user, just show the answer on the same page while doing the calculation server side.
My current app.js:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
app.set('port', process.env.PORT || 8080);
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '../static')));
app.get('/', function(req, res){
res.redirect('/public');
});
app.post('/add', function(req, res){
var a = parseFloat(req.body.numa);
var b = parseFloat(req.body.numb);
var sum = a+ b;
res.send('Sum: ' + sum);
});
var server = app.listen(app.get('port'), function(){
var port = server.address().port;
});
my HTML:
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
</head>
<body onload="testFunc()">
<h1>Public page</h1>
<form action="/add" method="post">
Number 1:
<input type="number" name="numa" step="any"/><br>
Number 2:
<input type="number" name="numb" step="any"/><br>
<input type="number" name="answer" readonly/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
I've looked through other posts but the solutions all work through doing it with pug or jade, is there any way to do this without? Any suggestions on what direction I should take would be appreciated! Thank you.