Unable to get req.body -- Express.JS
solving express.js post request issue
I encountered a problem on my express application, the problem was being unable to get the request body(req.body) after using the app.use(express.urlencoded({extended: false})
middleware, this issue persisted for a whole day, I was furious because the tutorial I was following ran cleanly.
I tried using Postman and I didn't get any errors, I tried console logging the req.body only got an empty object, signifying that my post request wasn't getting through, searched online found solutions that
required my application to use body parser app.use(express.bodyPaser)
, which is deprecated and is currently embedded in the urlencoded middleware.
The Solution
After a little digging, I found out that express introduced the JSON middleware that handles all JSON post request. Immediately after addingapp.use(express.json())
to my file, BOOOOM! It worked like magic.
I decided to share this solution because it bothered me for sometime and would want to provide solution to others who will encounter same issue.