Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!