Express.js req.body Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.body holds key-value pairs from the HTTP request body. By default it is undefined. Body-parsing middleware such as express.json() or express.urlencoded() fills it in.

Use it on POST, PUT, and PATCH routes after the parsers are registered with app.use(). It is a server-side property on the request object — browsers only send the raw body.

Syntax

Read req.body in your Express.js route handler. Start with this line.

Express
req.body
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
CallWhat it does
req.bodyPrimary signature.
app.use(express.json())Related call form.
app.use(express.urlencoded({ extended: true }))Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

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

Parameters

ParameterDescription
Body-parser middlewareRequired. Without it, req.body stays undefined
Matching Content-TypeRequired. JSON parser ignores form bodies; urlencoded ignores JSON
ValidationOptional. Never trust raw client fields

Return Value

req.body holds the parsed request body (object, string, Buffer, …) after body-parsing middleware runs. Without a parser, it is typically undefined.

Express
app.use(express.json());
app.post('/login', (req, res) => {
  const { email } = req.body; // object fields from JSON
});

Express.js Examples

Example 1 — Parse JSON and Form Bodies

Register both built-in parsers, then echo req.body (same pattern as the official docs).

Express
const express = require('express');
const app = express();

app.use(express.json()); // application/json
app.use(express.urlencoded({ extended: true })); // form posts

app.post('/profile', (req, res) => {
  console.log(req.body);
  res.json(req.body);
});

app.listen(3000);

How It Works

  1. Add parsers first. express.json() and express.urlencoded() run before the route.
  2. Match Content-Type. JSON requests hit the JSON parser; form posts hit urlencoded.
  3. Body becomes an object. Fields land on req.body as normal properties.
  4. Echo for learning. Production apps should validate before responding with the same data.

Example 2 — JSON Body with Simple Validation

Require a string name before creating a resource. Reject missing or wrong-type fields.

Express
const express = require('express');
const app = express();

app.use(express.json());

app.post('/users', (req, res) => {
  const name = req.body && req.body.name;
  if (typeof name !== 'string' || !name.trim()) {
    return res.status(400).json({ error: 'name is required' });
  }
  res.status(201).json({ message: 'User created', name: name.trim() });
});

app.listen(3000);

How It Works

  1. Enable JSON parsing. Only express.json() is needed for this API route.
  2. Read carefully. Check that req.body exists and name is a non-empty string.
  3. Reject bad input. Missing or wrong-type name returns 400 — avoids unsafe calls like .toString() on unknown values.
  4. Respond on success. Trim the string, then return 201 with the cleaned value.

Example 3 — HTML Form with urlencoded

Parse classic form posts. The browser sends application/x-www-form-urlencoded by default.

Express
const express = require('express');
const app = express();

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

app.get('/contact', (req, res) => {
  res.type('html').send(
    '
' + '' + '' + '
' ); }); app.post('/contact', (req, res) => { const email = req.body.email; if (typeof email !== 'string' || !email.includes('@')) { return res.status(400).send('Valid email required'); } res.send('Thanks, ' + email); }); app.listen(3000);

How It Works

  1. Use urlencoded middleware. extended: true supports richer form shapes when needed.
  2. Serve a form. method="POST" posts field names as body keys.
  3. Read req.body.email. The parser turns the encoded string into an object.
  4. Validate before use. A quick check avoids treating missing or odd values as safe strings.

Runtime Support

req.body is an Express.js request property filled by server middleware. It runs on Node.js (Express 5 needs Node.js 18+). Browsers only send the HTTP body.

✓ Server · Express 4 & 5

req.body

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.body Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Always mount a body parser before reading req.body.

Frequently Asked Questions

It holds key-value pairs from the request body after body-parsing middleware runs. By default it is undefined until you add express.json() or express.urlencoded().
Usually the parser middleware is missing, registered after the route, or the Content-Type does not match (for example sending JSON without express.json()).
Call app.use(express.json()) before your routes. It parses Content-Type application/json into a JavaScript object on req.body.
Use app.use(express.urlencoded({ extended: true })) for application/x-www-form-urlencoded form submissions.
No. It is user-controlled. Validate types and required fields before use — never assume req.body.foo.toString() will work.

Next: req.cookies

Read cookies sent with the request via cookie-parser middleware.

req.cookies tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful