pal deepak
pal deepak

Reputation: 55

How to calculate the basic sum of two numbers using nodejs restAPI through Postman

Following is my server.js file code,

var express = require('express');
console.log("A");
var app = express();
console.log("b");
var fs = require("fs");
console.log("c");
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
app.get('/listUsers', function (req, res) {
    var f = parseInt(req.body.a);
    console.log(req.body.a);
   var l = parseInt(req.body.b);
    var sum = Number(f + l);
    res.send('The sum is: ' + Number(sum));

})
var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port
   console.log("Example app listening at http://%s:%s", host, port)
})

When I provide the values of f and l using the postman following the url http://localhost:8081/listUsers,

The output I am getting is The sum is: NaN

Can you tell me why ? Am I missing something ?? Thanks

Upvotes: 0

Views: 1373

Answers (1)

Vinod Bhatia
Vinod Bhatia

Reputation: 111

Try using following as you are not receiving req body json

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json()); // add this line

Upvotes: 1

Related Questions