tried to console.log
data in node script from a http post
const express = require("express");
const app = express();
const port = 3700;
let io = require('socket.io').listen(app.listen(port));
const path = require('path');
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static('public'));
app.post('/do-login', function(req, res) {
console.log( req.body );
});
and in client, I'm using javascript fetch to send data to node backend
let data = {
email :'test@domain.com',
password : 'test'
}
fetch('http://localhost:3700/do-login',{
method : 'post',
body : JSON.stringify(data)
}).then(function(response) {
return response.json();
}).then(function(data) {
console.log(data);
});
but no luck, it returns an empty object
but, If I use post xmlHttpRequest
var http = new XMLHttpRequest();
var url = 'http://localhost:3700/do-login';
var params = 'email=test@domain.com&password=test';
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
I'm able to receive the data sent from client. What seems wrong?
Any help, ideas is greatly appreciated.