Yes, I've already looked at similar questions like this Express.js req.body undefined but it did not help me, and yes I've tried changing the code to a middleware.
My problem is that the passed XML is valid and everything is ok (I'm using Angular for Frontend), but my backend always delivers me an undefined variable req.body... I tried printing out console.log(req)
and it always shows the body as an empty object.
I have the following code:
var testvar = req.body;
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
res.on("error", function (error) {
console.error(error);
});
});
req.write(postData);
req.end();
console.log(testvar);
The code is implemented inside a app.post
, which naturally contains req, res
as usual. Options contain the IP, port etc, which is not relevant for solving this riddle.
Also: Once the post method is called, the error
TypeError: data should be a string, Buffer or Uint8Array
occurs in the backend.