title: Express - POST request pending
body: <p>I have a form to register users, but post request is not working</p>
  
  <p>I've checked bodyparser, consign includes order, added enctype to form and still do not work</p>
  
  <p>The route is working, cause it calls my controller, but as you can see at console image, it goes to the controller with undefined req, althought url params are defined at devtools</p>
  
  <p>server.js:</p>
  
  <pre><code>const express = require('express'); 
  const consign = require('consign');
  const bodyParser = require('body-parser');
  const expressValidator = require('express-validator');
  const helmet = require('helmet');
  const jwt = require('jsonwebtoken');
  require('dotenv-safe').load();  
  
  const app = express(); 
  app.set('view engine', 'ejs');
  app.set('views', './app/paginas'); 
  
  app.use(express.static('./app/publico/'));
  app.use(bodyParser.urlencoded({extended: true}));
  app.use(bodyParser.json());
  app.use(expressValidator());
  
  consign(/* {cwd: 'app'} */)
      .include('config/conectarBD.js')
      .then('app/modelos')
      .then('app/controles')
      .then('app/rotas')
      .into(app);
  
  
  /* consign()
      .include('app/rotas')
      .then('config/conectarBD.js')
      .then('app/modelos')
      .then('app/controles')
      .into(app); */
  
  console.log('Instância do app criada');
  
  module.exports = app;
  </code></pre>
  
  <p>form.ejs:</p>
  
  <pre><code>&lt;div class="row" id="form_registro"&gt;
                  &lt;div class="col-sm-6 offset-sm-3 text-center"&gt;
                          &lt;form action="/registrar" method="POST"&gt;
                          &lt;fieldset&gt;
                              &lt;legend&gt;Registro&lt;/legend&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="form-r-email"&gt;Nome:&lt;/label&gt;
                                  &lt;input type="text" class="form-control" id="nome" name="nome" placeholder="Seu nome"&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="form-r-email"&gt;Email:&lt;/label&gt;
                                  &lt;input type="email" class="form-control" id="email" name="email"
                                      placeholder="Seu email"&gt;
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="form-r-senha"&gt;Senha:&lt;/label&gt;
                                  &lt;input type="password" class="form-control" id="senha" name="senha"
                                      placeholder="Sua senha"&gt;
                              &lt;/div&gt;
                              &lt;!-- TODO implementar --&gt;
                              &lt;div class="form-check"&gt;
                                  &lt;input type="checkbox" class="form-check-input" id="exampleCheck2"&gt;
                                  &lt;label class="form-check-label" for="exampleCheck1"&gt;Lembre de mim&lt;/label&gt;
                              &lt;/div&gt;
                              &lt;button type="submit" class="btn btn-success"&gt;Registrar&lt;/button&gt;
                          &lt;/fieldset&gt;
                          &lt;div&gt;
                              Já tem uma conta?
                              &lt;a href="#" id="logar"&gt;Registrar!&lt;/a&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>route login.js:</p>
  
  <pre><code>module.exports = function(application) {
      application.get('/login', function(req, res) {
          console.log('Rota pegaPaginaLogin');
          application.app.controles.login.pegaPaginaLogin(application, req, res);
      });
      application.post('/admin/logar', function(req, res) {
          console.log('Rota /admin/logar');
          application.app.controles.login.logaUsuario(application, req, res);
      });
  
      application.post('/registrar', function(req, res) {
          console.log('Rota registrar');
          console.log('req.body &gt;&gt;&gt;' + req.body);
          res.status(500).send('testing');
          application.app.controles.login.registraUsuario(application, req, res);
      });
  }
  </code></pre>
  
  <p>controller registraUsuario:</p>
  
  <pre><code>module.exports.registraUsuario = function (application, req, res) {
      console.log('Controller registraUsuario');
      console.log('REQ....' + req)
      var usuario = req.body;
  
      /** Validação do formulário */
      //TODO validar formatos
      req.assert('nome', 'Nome é obrigatório').notEmpty();
      req.assert('email', 'Email é obrigatório').notEmpty();
      req.assert('senha', 'Senha é obrigatório').notEmpty();
  
      var errosValidacao = req.validationErrors();
  
      console.log(errosValidacao);
  
      if (errosValidacao) {
          res.render('login', {
              validacao: errosValidacao,
              usuario: usuario
          });
          return;
      }
  
      /** Conexão com banco */
      var conexao = application.config.conectarBD();
      var novoUsuario = new application.app.modelos.UsuariosModel(conexao);
  
      novoUsuario.getUsuario(usuario, function (error, result) {
          console.log(result);
  
          novoUsuario.novoUsuario(usuario);
      });
  
  }
  </code></pre>
  
  <p>model UsuariosModel:</p>
  
  <pre><code>function UsuariosModel(conexao) {
      this._conexao = conexao;
  }
  
  /** Se usuário existir, retorna a id_usuario */
  UsuariosModel.prototype.getUsuario = function (usuario, callback) {
  console.log('Model getUsuario');
  this._conexao.query('SELECT id_usuario FROM usuarios WHERE email = "' + usuario.email + ' and senha = "' + usuario.senha + '"');
  </code></pre>
  
  <p>}</p>
  
  <pre><code>UsuariosModel.prototype.novoUsuario = function (usuario, callback) {
      var hoje = Date.now();
      this._conexao.query('INSERT INTO usuarios SET ?', usuario, callback);
  }
  
  module.exports = function () {
      return UsuariosModel;
  };
  </code></pre>
  
  <p>console:</p>
  
  <p><a href="https://i.stack.imgur.com/vDEoW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vDEoW.png" alt="enter image description here"></a></p>
  
  <p>error:</p>
  
  <p><a href="https://i.stack.imgur.com/nboq6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nboq6.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Jm0pj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jm0pj.png" alt="enter image description here"></a></p>