You must use a body parser, otherwise your NodeJS application will not know how to interpret your string received in the request body.
npm install --save body-parser
and
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
Assuming you’re using Express.js (you should probably tag the question) you need to make sure you use the express.json() (or bodyParser.json()) middleware for JSON body parsing.
But the problem is, console.log(JSON.stringify(dataToSend)); returns {"username":"username1","password":"password1"} //or whatever the user input is as expected, whereas console.log(username, password, req.body) returns undefined undefined {}.
Does anyone know what I’m doing wrong?
Edit: I am using const app = express(); and I have app.use(bodyParser.urlencoded({ extended: true })); in my node.js file.
Comments
Comment posted by Quentin
Where’s the server-side JSON parser?
Comment posted by bgmn
Oh yes of course, how could I forget! Should I close this question since it may not be much help to others?