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

126
Vistas
express post method and body.req does not work

I am trying to get "POST" method form data from HTML form using app.post method but I can catch anything to req.body. What am I doing wrong?

My html form -

<form method="POST" action="/success.html">
  <div id="input-area">
    <textarea id="title" name="title" rows="1" cols="75" placeholder="Title" onkeyup="instance.tweaker()"></textarea>
    <textarea rows="10" type="text" cols="75" placeholder="Description" name="description"></textarea>
    <div id="calender"><input id="dater" type="date" value=""></div>
    <button type="submit" value="Submit"  id="buton">Add Task</button>
  </div>
</form>
<script src="backend2.js" defer></script>

my js code

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.listen(3308,() =>{
    console.log('My server is running');
})

const jsonParser = bodyParser.json();

//

const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.post('/success.html', urlencodedParser , function (req, res) {
    console.log(req.body.title);
})
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I reproduced your code without any error. To test it quickly, i send the index.html as the default route.

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');

const port = 3308;
const app = express();

const urlencodedParser = bodyParser.urlencoded({ extended: false });

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '/index.html'));
});

app.post('/success.html', urlencodedParser, (req, res) => {
    res.send(req.body.title);
});

app.listen(port, () => {
    console.info(`server running on port ${port}`);
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Upload Example</title>
  </head>
  <body>
    <form method="POST" action="/success.html">
      <div id="input-area">
        <textarea
          id="title"
          name="title"
          rows="1"
          cols="75"
          placeholder="Title"
          onkeyup="removedForExample"
        ></textarea>
        <textarea
          rows="10"
          type="text"
          cols="75"
          placeholder="Description"
          name="description"
        ></textarea>
        <div id="calender"><input id="dater" type="date" value="" /></div>
        <button type="submit" value="Submit" id="buton">Add Task</button>
      </div>
    </form>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza Denunciar

0

First of all, I am not sure which version of Express you are using but if you are using version 4.0 or up, you don't really need to import body-parser separately.

Also, you may re-order the code. Why don't you try like this?


const jsonParser = bodyParser.json();
const urlencodedParser = bodyParser.urlencoded({ extended: false })

app.post('/success.html', urlencodedParser , function (req, res) {
    console.log(req.body.title);
})

app.listen(3308,() =>{
    console.log('My server is running');
})

about 4 years ago · Juan Pablo Isaza 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