title: Form POST Requests Body is Empty - Node.js / express / ejs / body-parser
body: <p>I'm having trouble with my post from a registration form in node.js. Every time the form is posted the body the request has information but the body is empty. I'm new to node and express so it's probably something small but I can't for the life of me figure it out.</p>
  
  <p>Here is my form in ejs:</p>
  
  <pre><code>&lt;form class="form-signin" method="post" action="/users/register"&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="sr-only" for="email"&gt;Email address&lt;/label&gt;
      &lt;input type="email" class="form-control" id="email" placeholder="Enter Email" autocomplete="off"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="sr-only" for="password"&gt;Password&lt;/label&gt;
      &lt;input type="password" class="form-control" id="password" placeholder="Enter Password"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label class="sr-only" for="confirmPassword"&gt;Confirm Password&lt;/label&gt;
      &lt;input type="password" class="form-control" id="confirmPassword" placeholder="Re-enter Password"&gt;
    &lt;/div&gt;
    &lt;button class="btn btn-dark mt-3" type="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is my router code within the users router:</p>
  
  <pre><code>router.post('/register', function(request, response, next) {
    console.log(request);
    console.log(request.body)
    response.render('register', { 'title': 'Register' });
  });
  </code></pre>
  
  <p>Here is relevant output on the console:</p>
  
  <pre><code>console.log(request); output shortened this is just the tail.      
    _startAt: [ 299545, 483820242 ],
    _startTime: 2018-09-20T16:08:59.366Z,
    _remoteAddress: '::1',
    body: {},
    _body: true,
    length: undefined,
    read: [Function],
    secret: undefined,
    cookies: {},
    signedCookies: {},
    route:
     Route {
       path: '/register',
       stack: [ [Object] ],
       methods: { post: true } } }
  {}
  POST /users/register 200 21.906 ms - 2361
  </code></pre>
  
  <p>app.js code:</p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  var bodyParser = require('body-parser');
  
  var indexRouter = require('./routes/index');
  var usersRouter = require('./routes/users');
  
  var app = express();
  
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  
  app.use('/', indexRouter);
  app.use('/users', usersRouter);
  
  app.use(function(request, response, next) {
    next(createError(404));
  });
  
  app.use(function(error, request, response, next) {
    response.locals.message = error.message;
    response.locals.error = request.app.get('env') === 'development' ? error : {};
    response.status(error.status || 500);
    response.render('error');
  });
  
  module.exports = app;
  </code></pre>
  
  <p>Things I've tried:</p>
  
  <ul>
  <li>different browsers</li>
  <li>different routes</li>
  <li>writing simpler forms from scratch</li>
  <li>formidable instead of body-parser</li>
  </ul>
  
  <p>I'm pretty sure at this point that it's a problem with the ejs form because the body object in the request is always empty.</p>