Hi I have been dealing with a problem with my Express server. There have been many Stack Overflow posts on this problem but I have been unable to find an answer that works for me. When receiving a POST request, I am only getting {} for request.body in the code below:
const bodyParser = require('body-parser');
const app = express();
const User = require('./models/users');
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json()); // support json encoded bodies
//Basic routes
app.post('/signup', async (request,response)=>{
//const newUser = new User(queryObject);
console.log("in post");
console.log(request.body);
response.end();
});
//Binding to a port
app.listen(port, ()=>{
console.log('Express server started at port 3000');
});
This is where I am sending the POST request:
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => {
console.log(xhr.response);
});
xhr.open('POST', 'http://localhost:3000/signup', false);
xhr.send(JSON.stringify({ hello : "WORLD" }));
Is there something that I am missing or doing wrong? Thank you.