calculator app using node express
Calculator App
Q. How to get form data in express server?
Ans: Use Body-Parser Module to get html form data into the server.
Example:
If we wanna try to get from data we get form values in PayLoad section in Chrome Developer Tool
Here we can see :
firstNumber : 2
secondNumber: 6
these numbers are bound to forms input fields
To use these information in server We Need BodyParser
> npm install body-parser
> const bodyParser = require("body-parser");
Then use it:
> app.use(bodyParser.urlencoded({extended: true}));
here urlencode() is function which give form data.
IF we console the request like this :
Then we get output like : { firstNumber: '7', secondNumber: '1' }
To get individual values, Use:
So the complete code for calculator will be like this :
and if num1 = 2 and num2 = 5 the output will be 25 because + sign here concatenate and thinks it Strings value.
So we have to parse it as a number. use parseInt() function.
Comments
Post a Comment