When I send data from Angular to NodeJS using http.post, it always returns me empty req.query. My server.js:
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.post('/', (req, res) => {
console.log(req.query);
res.send(req.query);
});
app.listen(4000, () => {
console.log('Successfully');
});
Frontend:
onSubmit(f: NgForm) {
let data = f.value;
let httpOptions = {
headers: new HttpHeaders().set('Content-Type', 'application/json')
};
data = JSON.stringify(data);
this.http.post('http://localhost:4000/', data, httpOptions).subscribe(data => {
console.log(data);
}, error => {
console.log(error);
});
}
And it always returns {} in console. I tried to run POST-requests in POSTMAN and it correctly worked there. Here is the body of http-request in browser: http-request in browser
Hope for your help, thanks.