Express.js
Node.js
JavaScript
Web Development
Form Handling

How to access POST form fields in Express

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Express, POST form fields are usually read from req.body, but that only works after you add the right body-parsing middleware. The exact middleware depends on the request content type: URL-encoded forms, JSON bodies, and multipart form uploads are handled differently.

Standard HTML Forms Use express.urlencoded

Traditional browser forms usually submit as application/x-www-form-urlencoded. For that case, enable:

javascript
1const express = require('express');
2const app = express();
3
4app.use(express.urlencoded({ extended: true }));
5
6app.post('/submit', (req, res) => {
7  console.log(req.body);
8  console.log(req.body.username);
9  console.log(req.body.email);
10  res.send('ok');
11});
12
13app.listen(3000);

With a matching HTML form:

html
1<form method="post" action="/submit">
2  <input name="username">
3  <input name="email">
4  <button type="submit">Send</button>
5</form>

After submission, the fields are available on req.body.

JSON Requests Use express.json

If the client sends JSON instead of an HTML form:

javascript
1app.use(express.json());
2
3app.post('/api/users', (req, res) => {
4  console.log(req.body);
5  res.json({ received: req.body });
6});

This is common for frontend applications using fetch, Axios, or mobile clients.

The key idea is that Express does not magically parse every body format the same way. The middleware must match the content type being sent.

Multipart Forms Need Specialized Middleware

File uploads and multipart forms are different. express.urlencoded and express.json are not enough. A common solution is multer:

javascript
1const multer = require('multer');
2const upload = multer({ dest: 'uploads/' });
3
4app.post('/upload', upload.single('avatar'), (req, res) => {
5  console.log(req.body);
6  console.log(req.file);
7  res.send('uploaded');
8});

In that case:

  • text fields are still available on req.body
  • uploaded file metadata appears on req.file or req.files

Middleware Order Matters

The parser must be registered before the route:

javascript
1app.use(express.urlencoded({ extended: true }));
2
3app.post('/submit', (req, res) => {
4  res.send(req.body.username);
5});

If the middleware is added after the route, req.body will be undefined or empty for that handler.

That ordering mistake is one of the most common causes of "why is req.body empty?"

Example With Validation

A small validation example makes the normal flow clearer:

javascript
1app.use(express.urlencoded({ extended: true }));
2
3app.post('/register', (req, res) => {
4  const { username, email } = req.body;
5
6  if (!username || !email) {
7    return res.status(400).send('Missing required fields');
8  }
9
10  res.send(`Registered ${username}`);
11});

This is the usual pattern: parse, destructure, validate, then use the fields.

If you are debugging an empty body, inspect the request's Content-Type header first. A form sent as JSON, multipart, or URL-encoded data needs matching middleware, and a mismatch is often the real reason the fields never appear.

It is also worth remembering that middleware order affects every downstream route. If you split your Express app into routers, make sure the body parser is applied at the app level or mounted before the router that needs req.body.

For security-sensitive forms, validate and sanitize the fields after parsing instead of assuming middleware parsing means the values are trustworthy. Parsing only turns bytes into JavaScript objects; it does not guarantee that the content is safe or complete.

That matters in production.

Common Pitfalls

One common mistake is trying to read req.body without any body-parsing middleware.

Another issue is using express.urlencoded for JSON requests or express.json for classic form posts and expecting both to behave the same.

A third problem is forgetting that multipart form uploads need different middleware such as multer.

Finally, developers often place the parser middleware after the route declarations and then spend time debugging an empty req.body.

Summary

  • POST form fields in Express are usually read from req.body.
  • Use express.urlencoded for standard HTML form posts.
  • Use express.json for JSON request bodies.
  • Use multipart middleware such as multer for file-upload forms.
  • Make sure the parser middleware is registered before the route handlers that need it.
  • Match the parser to the incoming content type before reading form fields.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.