I am trying to send a 'POST' XMLHttpRequest to my server, which uses express. The server is able to recieve the request, but the request's body is null.
On the server I am receiving it with :
app.post('/', function(req, res){
res.send(req.statusCode);
console.log(req.body);
})
I don't think that there is a problem with it as many sources say that this is a good way to receive the request.
This is my client side code :
const user = {
'Key' : 'user',
'Name' : 'user',
'Email' : 'user@example.com',
'Password' : 'password'
}
const jsonFileRequest = new XMLHttpRequest();
jsonFileRequest.open('POST', '/', true);
jsonFileRequest.setRequestHeader('Content-Type', 'application/json');
jsonFileRequest.onreadystatechange = function(){
console.log(`userStatus : ${jsonFileRequest.readyState},\nstatus: ${jsonFileRequest.status}`);
if(jsonFileRequest.readyState == 4 && jsonFileRequest.status == 200)
{
console.log(`userStatus : ✅,\nstatus : ✅`);
}
}
jsonFileRequest.send(JSON.stringify(user));
**This is my server side code : **
const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/frontend/index.html');
app.get('/index.html.js', (req, res) => {
res.sendFile(__dirname + '/frontend/index.html.js');
})
app.get('/data.json', (req, res) => {
res.sendFile(__dirname + '/data.json');
})
})
app.post('/', function(req, res){
res.send(req.statusCode);
console.log(req.body);
})
app.listen(port);
console.log(`listening on http://localhost:${port}`);
Could you please help me? Thank you!