antonig
antonig

Reputation: 163

Node.js express routing POST Request empty body, using body-parser

I am having an issue where my post requests from a submission form are producing an empty body, no matter what body parser settings I use. My package.json dependencies are all up to date, and a previous app I made (with the deprecated app.use(bodyParser);) does this with no issues. I have been pouring over SO and various sites and trying every hack, but nothing seems to work.

Here's the relevant parts of my app.js:

var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var routes = require('./routes/routes.js');

app.post('/checklogin',routes.post_checklogin); 

Here's the route function that's called on post request:

var checkLogin = function(req,res) {
    console.log(req.body)
}

Here's the form I'm trying to get my form from, templated in pug:

form(action="/checklogin" method="post" enctype="application/x-www-form-urlencoded")
    input(id="login-email" class="form-control" type="text" placeholder="email")
    input(id="login-password" class="form-control" type="text" placeholder="password")
    button(id="login-submit" class='btn btn-primary') Login

No matter what combination of enctype or body parser settings allow me to have anything but '{}' as the req.body. If anyone could point out what I imagine is a really stupid bug, I would be eternally grateful.

Also, I realize this is probably not the best way to handle logins, but this is just to get POST requests working in general throughout the app.

Upvotes: 1

Views: 648

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

It's not a body-parser issue, you're missing the name attribute on your input elements.

Taken from W3C specification:

A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

input(id="login-email" name="email" class="form-control" type="text" placeholder="email")
input(id="login-password" name="password" class="form-control" type="text" placeholder="password")

Upvotes: 1

Related Questions