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

157
Vistas
req.body returns {} in post when it shouldn't?

I have been tasked with creating a small database of users for someone, and I'm using MySQL alongside with Node.js in order to do it My idea was pretty simple - a form in a .ejs file, in it 2 parameters - username and passwords, and when someone enters the details, in an app.post(), it inserts the entered information from the form into the database and sends the user to a temporary "Done Successfully" site. Here is the part of the code that matters:

The form:

<form class="form-signup" action="/successful" target="_blank" method="post" enctype="multipart/form-data">
    <label for="username">Username:</label>
    <input type="text" name="username" placeholder="Type your username" required>
    <br>
    <label for="password">Password:</label>
    <input type="password" name="password" placeholder="Type your password" required>
    <br>
    <button class="center" type="submit">Sign Up</button>
</form>

app.js:

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

app.set('view engine', 'ejs');
app.use(body_parser.urlencoded({extended: true}));
app.use(body_parser.json());

app.listen('3000', function() {
    console.log('Server is running at localhost:3000');
});

...

app.post('/successful', function (req, res) {
    let user = {
        username: req.body.username,
        password: req.body.password,
        created: new Date()
    }
    let sql = "INSERT INTO usernames SET ?";
    let query = db.query(sql, user, function (err, result) {
        if (err) throw err;
        else console.log(req.body);
    });
    res.status(200).render('successful');
});

(I did connect the database, just didn't show it)

And yet req.body returns {}. I would appreciate any amount of help. Thanks in advance!

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