I am making a simple calculator that does calculation on server as a beginner project. This is the code I have written in .js
file:
const express = require("express");
const bodyParser=require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function (req, res) {
res.sendFile(__dirname "/index.html");
});
app.post("/",function(req,res){
var num1=Number(req.body.num1);
var num2=Number(req.body.num2);
var num3=num1 num2;
res.send(num3); // gives error if i don't write it as string >>like
//res.send("your answer is" num3); >> this one doesn't throw an error
})
app.listen(3001, function () {
console.log("server started at port 3001");
});
Why is this so? I am a beginner so please explain simply.
CodePudding user response:
This problem is happening because you are giving send
a parameter which is a Number
, that isn't supported by the method. Here is what the doc says:
res.send([body])
The
body
parameter can be aBuffer
object, aString
, anObject
,Boolean
, or anArray
.