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

653
Vistas
req.body returning empty object (I've included bodyparser)

I'm new to backend javascript and I have a form that on submit is supposed to show the submitted object, but it's displaying an empty object instead. I've included body-parser so I'm not sure where the issue is. Here is my app.js file:

const express = require("express");
const app = express();
const mustacheExpress = require("mustache-express");
const bodyParser = require("body-parser");
const pgp = require("pg-promise")();

app.engine("html", mustacheExpress());
app.set("view engine", "html");
app.set("views", __dirname + "/views"); 
app.use("/", express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/new_creature',function(req,res){
    res.render('new_creature/index');
});
app.post('/new_creature', function(req,res){
    res.end(JSON.stringify(req.body));
    console.log(req.body);
    // res.render('new_creature/index');
});

and here is my views/creatures/new_creature/index.html file:

<form method="POST" action="/new_creature">
    Species:<input type="text">
    Family:<input type="text">
    Habitat:<input type="text">
    Diet:<input type="text">
    Planet:<input type="text">
    <input type="submit" placeholder="submit">
</form>
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you want bodyparser to work you need to have name attributes on your form elements that you want posted. For example:

<form method="POST" action="/new_creature">
    Species:<input name='species' type="text">
    Family:<input name='family' type="text">
    Habitat:<input name='habitat' type="text">
    Diet:<input name='diet' type="text">
    Planet:<input name='planet' type="text">
    <input type="submit" value="submit">
</form>

Also, placeholder doesn't make sense on a submit button; placeholder is the ghost text that's supposed to show in an input that hasnt' been filled.

about 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