Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
req.body is always undefined in Express/bodyParser/NodeJS

I'm having an issue where my req.body is always undefined when I do a POST. I'm using NodeJS, Express and BodyParser. Everything else I can find online says I need the include .json(), as below on the 1st line, but this didn't sort the issue. I've also tried with {extended: true} which also didn't help.

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(
    session({
        secret: 'my secret',
        resave: false,
        saveUninitialized: false,
        store: site
    })
);

If I run a line of code like req.body.text I'll just get undefined as the result. Does anyone know what my mistake might be?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

body parser is deprecated see https://stackoverflow.com/a/68127336/10772577.

Sending a post request to a route with 'Content-Type': 'application/json' should give you a body.

Example express route.

app.post('/api', function(request, response){
   console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});

See https://expressjs.com/en/guide/routing.html

Example client request.

const response = await fetch('/api', {
    method: 'POST',
    body: JSON.stringify(object),
    headers: {
      'Content-Type': 'application/json'
    }
});

See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

over 4 years ago · Santiago Trujillo Denunciar

0

const app = express();
/* EXPRESS BODY PARSER */
app.use(express.json());
const myFunction(req,res) = async (req,res) => {
  const body = req.body;
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda