Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
express.js req.body undefined in middleware

I'm currently facing a problem in my node application.

while trying to use middleware in a post request the req.body becomes undefined.

for example;

router.post('/newpost', ensureAuthenticated, createPost, upload.single('file'), async (req, res) =>{
    console.log(req.body);
}

async function createPost(req, res, next){
    console.log(req.body);
    next();
}

when the createPost function runs it logs req.body as undefined. but when req.body gets logged inside the router.post it is defined.

is it something i'm missing? or is this just not possible to do.

also i've ensured that i've included bodyparser and initialized it before my routes.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

well, I've just tested and all works fine, with my suggestion in the comment

here's the content of my test:

my index.js

const express = require('express')

const router = express.Router()
const app = express()
const PORT = process.env.PORT || 3002

app.use(express.json())

const createPost = (req, res, next) => {
    console.log('createPost', req.body)
    next()
}

router.post('/newpost', createPost, (req, res) => {
    console.log('/nextpost', req.body)
    res.json({ message: 'ok' })
})

app.use('/', router)

app.listen(PORT, () => {
    console.log(
        `server ready at http://localhost:${PORT}`
    )
})

and a simple REST Client file

@HOST = http://localhost:3002

POST {{HOST}}/newpost
Content-Type: application/json

{
    "fname": "Bruno",
    "lname": "Alexandre",
    "nick": "balexandre"
}

and the result is

❯ node .\index.js
server ready at http://localhost:3002
createPost { fname: 'Bruno', lname: 'Alexandre', nick: 'balexandre' }
/nextpost { fname: 'Bruno', lname: 'Alexandre', nick: 'balexandre' }

and the response of the call

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 16
ETag: W/"10-/VnJyQBB0+b7i4NY83P42KKVWsM"
Date: Tue, 26 Jan 2021 19:59:21 GMT
Connection: close

{
  "message": "ok"
}

screenshot (click for full image)

enter image description here


Warning

make sure you're passing Content-Type: application/json in your POST request, remember that you told Express that you wanted the body parsed as .json() so make sure it knows you're passing json as the request body

a bit more information... the req.body is only undefined if I do not use the parser, like:

enter image description here


Added

a GitHub repository with working solution > https://github.com/balexandre/so65907925

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!