title: How to get data from post form with express.js server
body: <p>Problem:
  When the client sumbit form with post reqest to the server, the express server gets an empty body (req.body = {}).</p>
  <p>What I am trying to do:
  To get req.body.username and req.body.password on post reqest from the client (sending the form down below)</p>
  <p>Server.js (Main app file)</p>
  <pre><code>var express = require('express')
  var http = require('http')
  
  const mongoose = require('mongoose')
  const { json } = require('express')
  var cookieParser = require('cookie-parser')
  
  var app = express()
  var server = http.createServer(app)
  
  const bodyParser = require('body-parser')
  
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  
  const formidable = require('formidable');
  
  mongoose.connect('mongodb://localhost:27017/grabber', {useNewUrlParser: true, useUnifiedTopology: true});
  
  app.use(cookieParser())
  
  app.set(&quot;view engine&quot;, &quot;ejs&quot;)
  
  app.set('views', __dirname+'/views/html/');
  
  app.use(&quot;/js&quot;, express.static(__dirname + &quot;/views/js&quot;))
  app.use(&quot;/css&quot;, express.static(__dirname + &quot;/views/css&quot;))
  
  const registerRoute = require(&quot;./routes/register&quot;)
  const grabberRoute = require(&quot;./routes/grabber&quot;)
  const downloadRoute = require(&quot;./routes/download&quot;)
  const adminRoute = require(&quot;./routes/admin&quot;)
  
  app.use(&quot;/register&quot;, registerRoute)
  app.use(&quot;/id&quot;, grabberRoute)
  app.use(&quot;/download&quot;, downloadRoute)
  app.use(&quot;/admin&quot;, adminRoute)
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
      
      res.redirect(&quot;/register&quot;)
  })
  
  
  app.use(function (err, req, res, next) {
      res.status(400).send(&quot;Error code: 2 &lt;hr&gt; This page is currently inaccessible! &lt;br&gt; &lt;a href='/'&gt;GO TO HOMEPAGE&lt;/a&gt;&quot;)
  
      console.log(err)
  })
  
  
  
  server.listen(80)
  </code></pre>
  <p>The route I have a problem on (On the post request)</p>
  <pre><code>const express = require('express')
  const router = express.Router()
  const kSchema = require(&quot;../models/key&quot;)
  const Schema = require(&quot;../models/adminCredentials&quot;)
  const fs = require('fs');
  const formidable = require('formidable');
  
  router.get(&quot;/&quot;, (req, res) =&gt; {
  
  
      checkCookie(req.cookies['credentials'], cookieCb)
  
      function cookieCb(f){
  
          if(f != null &amp;&amp; f != undefined &amp;&amp; f !=&quot;&quot;){
              res.render(&quot;admin&quot;, {})
          }else{
              res.render(&quot;adminLogin&quot;, {})
          }
      }
  
  })
  
  async function checkCookie(cookieV, cb){
  
      let found = Schema.findOne({Session: cookieV}, function(err,obj){}).then(f =&gt; cb(f))
  }
  
  router.post(&quot;/loginInput&quot;, (req, res) =&gt; {
  
      console.log(req.body) 
     
  })
  
  module.exports = router 
  </code></pre>
  <p>The html/ejs file I am sending to the client</p>
  <pre><code>&lt;html&gt;
      &lt;html lang=&quot;en&quot;&gt;
  
      &lt;head&gt;
          &lt;meta charset=&quot;UTF-8&quot;&gt;
          &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  
          &lt;title&gt;Login&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;h1&gt;Login&lt;/h1&gt;
  
          &lt;form action=&quot;/admin/loginInput&quot; method=&quot;POST&quot;&gt;
              &lt;input name=&quot;username&quot; id=&quot;username&quot; class=&quot;username&quot; placeholder=&quot;Username&quot;&gt;
              &lt;input name=&quot;password&quot; id=&quot;password&quot; class=&quot;password&quot; placeholder=&quot;Password&quot;&gt;
              &lt;button name=&quot;password&quot; type=&quot;submit&quot; class=&quot;submit&quot;&gt;Login&lt;/input&gt;
          &lt;/form&gt;
  
          
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <hr />
  <p>What I am using:</p>
  <p>body-parser - 1.19.0</p>
  <p>connect-busboy - 0.0.2</p>
  <p>cookie-parser - 1.4.5</p>
  <p>crypto-js 4.0.0</p>
  <p>ejs 3.1.5</p>
  <p>express 4.17.1</p>
  <p>mongoose 5.11.19</p>
  <hr />