Node.jsExpressError Fix2026 Updated

req.body undefined in Express— Fixed [Complete Guide 2026]

January 20266 min readExpress 4.x · Node.js · All OS

⚡ Quickest Fix — Two Lines

Add these two lines to your Express app BEFORE all routes:

server.js / app.js
const express = require('express')
const app = express()

// ← ADD THESE TWO LINES BEFORE ROUTES
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

// Routes come AFTER
app.post('/api/users', (req, res) => {
  console.log(req.body)  // now populated
  res.json(req.body)
})

Still undefined? Check that your frontend is sending Content-Type: application/json. See Fix 3.

Why Is req.body undefined in Express?

Terminal Output
// console.log(req.body) inside your route:
undefined

// or an empty object even though you sent data:
{}

Express does not parse request bodies by default. You must explicitly add middleware to tell Express how to read the request body. Without it, req.body is always undefined.

SymptomCauseFix
req.body is undefinedBody parser middleware missingJump →
req.body is undefined for some routesMiddleware added after routesJump →
req.body is {} (empty object)Frontend not sending Content-TypeJump →
req.body is undefined in old ExpressExpress below 4.16Jump →
req.body undefined for file uploadsmultipart/form-data not handledJump →
413 Request Entity Too LargeBody size limit exceededJump →
1

Add Body Parser Middleware

Express 4.16+ — no extra packages needed
1 min

Since Express 4.16, body parsing is built in. You do not need to install any additional packages.

server.js — complete working example
const express = require('express')
const app = express()

// Parse JSON bodies (application/json)
app.use(express.json())

// Parse URL-encoded bodies (application/x-www-form-urlencoded)
// e.g. HTML form submissions
app.use(express.urlencoded({ extended: true }))

// Routes AFTER middleware — always
app.post('/api/users', (req, res) => {
  console.log(req.body)  // { name: 'John', email: 'john@example.com' }
  res.status(201).json({ created: req.body })
})

app.post('/contact', (req, res) => {
  console.log(req.body)  // form data from HTML form
  res.json({ received: true })
})

app.listen(3000)

What each middleware handles

MiddlewareHandlesContent-Type
express.json()JSON data from fetch/axiosapplication/json
express.urlencoded()HTML form submissionsapplication/x-www-form-urlencoded
multer (separate package)File uploadsmultipart/form-data

Add both express.json() AND express.urlencoded() — they handle different request types and do not conflict with each other.

2

Middleware Must Go Before Routes

Works for some routes but not others
1 min

Express applies middleware in the order it is defined. If body parser is added after a route, that route never has access to req.body.

server.js
// ❌ WRONG — body parser after routes
const app = express()

app.post('/users', (req, res) => {
  console.log(req.body)  // undefined!
  res.json(req.body)
})

app.use(express.json())   // too late — route already defined above

// ✅ CORRECT — body parser first, routes second
const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.post('/users', (req, res) => {
  console.log(req.body)  // populated!
  res.json(req.body)
})

The correct order: 1. Security middleware (helmet, cors), 2. Body parsers (express.json, urlencoded), 3. Logging middleware, 4. Routes, 5. Error handler.

3

Fix Frontend Content-Type Header

req.body is empty object {}
2 min

Even with body parser middleware, req.body will be empty if your frontend does not send the correct Content-Type header. Express uses this header to decide which parser to apply.

fetch API — must set Content-Type manually

Frontend JavaScript
// ❌ WRONG — no Content-Type header
fetch('/api/users', {
  method: 'POST',
  body: JSON.stringify({ name: 'John' })
  // missing Content-Type → req.body is {}
})

// ✅ CORRECT — Content-Type: application/json
fetch('/api/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ name: 'John' })
})

Axios — sets Content-Type automatically

Frontend JavaScript
// ✅ Axios automatically sets Content-Type: application/json
// when you pass an object as the request body
axios.post('/api/users', { name: 'John' })

// ✅ Axios also handles arrays correctly
axios.post('/api/items', [1, 2, 3])

HTML form — sets Content-Type automatically

HTML form
<!-- HTML forms send application/x-www-form-urlencoded by default -->
<!-- express.urlencoded() handles this -->
<form method="POST" action="/contact">
  <input name="name" value="John" />
  <button type="submit">Submit</button>
</form>

Debug: log the Content-Type header

server.js — temporary debug
app.post('/api/users', (req, res) => {
  console.log('Content-Type:', req.headers['content-type'])
  console.log('req.body:', req.body)
  // Expected: Content-Type: application/json
  // If undefined or different → fix the frontend
  res.json({ received: req.body })
})
4

Express Below 4.16 — Install body-parser

Older Express versions
2 min

If you are using Express 4.15 or below (check: npm list express), the built-in body parser is not available. Install the separate body-parser package.

terminal
npm install body-parser
server.js
const express = require('express')
const bodyParser = require('body-parser')

const app = express()

// body-parser for older Express versions
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

Express 4.16+ is now almost universal. If you are on an older version, upgrading to the latest Express is a better long-term fix: npm install express@latest

5

File Uploads — multipart/form-data

req.body undefined for file upload routes
5 min

express.json() and express.urlencoded() do NOT handle file uploads. For multipart/form-data (file uploads), you must use multer.

terminal
npm install multer
routes/upload.js
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })

// Single file upload
app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.file)     // uploaded file info
  console.log(req.body)     // other form fields
  res.json({ uploaded: req.file.filename })
})

// Multiple files
app.post('/photos', upload.array('photos', 5), (req, res) => {
  console.log(req.files)    // array of uploaded files
  res.json({ count: req.files.length })
})

// Mixed: files + form fields
app.post('/profile', upload.fields([
  { name: 'avatar', maxCount: 1 },
  { name: 'gallery', maxCount: 10 }
]), (req, res) => {
  console.log(req.files)    // { avatar: [...], gallery: [...] }
  console.log(req.body)     // text fields from the form
  res.json({ success: true })
})
Frontend — sending files
// FormData for file uploads
const formData = new FormData()
formData.append('file', fileInput.files[0])
formData.append('name', 'My Photo')

// Do NOT set Content-Type manually with FormData
// The browser sets it automatically with the correct boundary
fetch('/upload', {
  method: 'POST',
  body: formData
})

Never set Content-Type manually when using FormData. The browser automatically adds the multipart/form-data boundary that multer needs to parse the file correctly.

6

Increase Body Size Limit (413 Error)

PayloadTooLargeError / 413 Request Entity Too Large
1 min
Terminal Output
PayloadTooLargeError: request entity too large
    at readStream (/node_modules/raw-body/index.js:155:17)

The default body size limit is 100kb. Large JSON payloads or base64-encoded files exceed this. Increase the limit:

server.js
// Increase limit for JSON bodies
app.use(express.json({ limit: '10mb' }))

// Increase limit for URL-encoded bodies
app.use(express.urlencoded({ extended: true, limit: '10mb' }))

// Or set a very high limit (use carefully — large payloads can be a DoS risk)
app.use(express.json({ limit: '50mb' }))

Set the smallest limit that covers your use case. Very large body limits can be exploited for denial-of-service attacks. For file uploads over a few MB, use multer with disk storage instead of base64-encoded JSON.

7

req.body is Empty Object {}

Middleware working but body appears empty
2 min

If req.body is {} (not undefined — actually an empty object), the middleware is working correctly but the frontend is either not sending data or sending with the wrong Content-Type.

Debug step by step

server.js — add debug middleware
// Add this BEFORE your routes to debug all requests
app.use((req, res, next) => {
  console.log('Method:', req.method)
  console.log('Content-Type:', req.headers['content-type'])
  console.log('Body:', req.body)
  next()
})

Common reasons for empty body

  • Frontend sending GET request instead of POST — GET requests have no body
  • Content-Type: text/plain instead of application/json — Express ignores it
  • JSON.stringify() not called — sending [object Object] as text
  • Empty object being sent: {} — this is valid, req.body will be {}
  • Query parameters confused for body — use req.query not req.body for URL params

Debug Checklist

  1. 1

    Is app.use(express.json()) present?

    Add it before all routes

  2. 2

    Is it placed BEFORE your routes?

    Move it to the top of middleware chain

  3. 3

    Is Content-Type: application/json set in the request?

    Add to fetch headers or use axios

  4. 4

    Is it a file upload? (multipart/form-data)

    Use multer instead of express.json()

  5. 5

    Is the body over 100kb?

    Increase: app.use(express.json({ limit: '10mb' }))

  6. 6

    Is it still {} after all fixes?

    Log Content-Type header to confirm what is being sent

How to Prevent This Error

Frequently Asked Questions

Why is req.body undefined in Express?+
Body parser middleware is missing or placed after your routes. Add app.use(express.json()) and app.use(express.urlencoded({ extended: true })) BEFORE all route definitions.
Do I need to install body-parser in Express 4?+
No. Express 4.16+ has body-parser built in. Use app.use(express.json()) instead of installing the separate body-parser package. Only use body-parser if you are on an older Express version.
req.body is an empty object {} — why?+
Your frontend is sending the request without the correct Content-Type header. For JSON add: headers: { 'Content-Type': 'application/json' }. Axios sets this automatically when you pass an object.
How do I receive file uploads in Express?+
express.json() does not handle multipart/form-data (file uploads). Install multer: npm install multer. Then: const upload = multer({ dest: 'uploads/' }); app.post('/upload', upload.single('file'), handler).
req.body works for some routes but not others?+
The body parser middleware is added after some routes. Move app.use(express.json()) to the very top of your middleware chain, before any route definitions.
Express body parser 413 error — how to fix?+
Your request body exceeds the default 100kb limit. Increase it: app.use(express.json({ limit: '10mb' })) and app.use(express.urlencoded({ extended: true, limit: '10mb' })).

Need Expert Help?

We Build Production-Grade Express & Node.js APIs

Softplix is a full-stack development agency specialising in Node.js backends. Dealing with Express configuration issues? We can help.

Talk to an Engineer