I have 2 js files that correspond with each other, one for the server and one for the client. The client should use the post request named "/postGetInc", send a number to the server, the server increments it and returns the result. Whenever I try to post the specific number to the server it sees it as an empty object. I know that the input should also be an object, but when I make an object using {"number": number} it still doesn't recognize the number value.
So this is the little code on my server.js
app.post("/getInc", function (req, res) {
console.log("req: " + req);
console.log("req.number: " + req.number);
req.number++;
res.send(req.number);
});
and this is the function I use for the post, on the client.js
function postGetInc(number){
var res = "no response";
toSend = { "number": number };
$.post("/getInc", toSend, function (response) {
console.log(response);
res = response;
});
return res;
//this should return number+1
//in case of a failt, it returns "no response"
}
whenever I run postGetInc(10) I get
"no response"
printed on my console.
on my server console I get:
req: [object Object]
req.number: undefined
If I replace {"number": number } with just number, and req.number with number, I get the same messages printed on my console.
Thanks for reading, I'd love to hear what I do wrong so please help me.