In this problem, I want to pass my values in express js so that it will written down in node js. I am clicking a button which will be the demo here
<form action="/measure" method="POST">
<button type="button" name="Add" > Add </button>
<button type="button" name="Substract" > Subtract </button>
<input type="submit">
</form>
I did something like this in express js
const homePage = fs.readFileSync('./index.html')
app.get('/',(req,res) => {
res.writeHead(200,{'content-type':'text/html'})
res.write(homePage)
res.end()
})
var counter = 0;
app.post('/measure',(req,res) => {
res.writeHead(200,{'content-type':'text/html'})
const {Add,Subtract,result} = req.body
if (Add) {
counter += 1;
} else if (Subtract) {
counter -= 1
}
res.write(homePage + `<h1>${counter}</h1>`)
res.end()
})
But this doesn't do anything, based on my thought, the wrong about here is the button in index html which it doesn't pass anything...Have you guys any idea how I can pass the value in the express js? I've been searching since last night about it but couldn't find the right information, All I get is this link which is not my prefer since I am not focusing yet in database LINK
A single button in a form can be represented as a <button> element. But if you want several buttons in a form, represent them all as <input type="submit">:
<form action="/measure" method="POST">
<input type="submit" name="Add" value="Add"/>
<input type="submit" name="Subtract" value="Subtract"/>
<input type="submit"/>
</form>
Depending on which button you press, you will get req.body as
{"Add": "Add"} or{"Subtract": "Subtract"} or{}