I'm trying to read data sent to node.js server.
Client side:
const sendToDB =(date)=> {
var xhttp = new XMLHttpRequest();
var d = JSON.stringify(date);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(d);
}
};
xhttp.open("POST", "api/info", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(d);
}
and the server:
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
req.on('data', function (chunk) {
console.log(req.body.date);
});
res.end();
});
server.listen(3001);
how to receive data sent with xhttp.send(d) in node js server ?