1

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.

1 Answer 1

3

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 a Buffer object, a String, an Object, Boolean, or an Array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.