I´m using nodejs to make a server what connect with a DB and a simple website, i can do a GET fetch and i take the data, but when i do a POST fetch my server receive and empty request.body.I try 2 diferents versions of nodejs 12.16.1 and 16.10.0 also i use express and body-parser and i got the same result.
This is my config on the server site
const express = require('express');
const morgan = require('morgan');
const app = express();
const router = require('../routes/routes.js');
/**configuraciones */
app.set('port',3000);
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(router);
app.listen(app.get('port'), () =>{
console.log("El server esta operativo ")
});
This is my code on website
var username = document.getElementById('user-reg');
var emailreg = document.getElementById('email-reg');
var btnReg = document.getElementById('btn-reg')
btnReg.addEventListener('click', sacarInformacion)
function sacarInformacion(){
var user = username.value
var email = emailreg.value
var jsonString = `{"correo": "${email}", "nombre":"${user}"}`
const options = {
method : 'POST',
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.parse(jsonString)
}
fetch('http://localhost:3000/to', options);
console.log(JSON.parse(jsonString))
//i catch in the console the data i pass on body
}
The main problem is that i used postman as API and i get the body on the server, but when i use it on website i get an empty body