Built-In Middleware
Built-in middleware in Express are the ready-made middleware functions that ship with the framework itself, so you can use them without installing extra packages. In Express 4 there are three: express.json(), express.urlencoded() and express.static(), plus express.raw() and express.text() added in later 4.x releases. They handle everyday jobs such as reading request bodies and serving files like images, CSS and front-end builds.
Where built-in middleware fits
Every request in Express passes through a chain of middleware functions in the order you register them with app.use(). Each one can read or change the request, end the response, or call next() to hand control to the next function. Built-in middleware are simply functions Express provides for that chain, configured by calling them with options.
Parsing request bodies
By default, req.body is undefined. Express does not read the incoming body until a parser runs.
const express = require('express');
const path = require('path');
const app = express();
// JSON bodies, e.g. from fetch() with Content-Type: application/json
app.use(express.json({ limit: '100kb' }));
// HTML form posts: Content-Type application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// Files from ./public, e.g. /logo.png serves public/logo.png
app.use(express.static(path.join(__dirname, 'public'), { maxAge: '1d' }));
// Mount under a prefix: /assets/app.css serves dist/app.css
app.use('/assets', express.static(path.join(__dirname, 'dist')));
app.post('/api/notes', (req, res) => {
const { title } = req.body;
if (!title) return res.status(400).json({ message: 'Title is required' });
res.status(201).json({ id: Date.now(), title });
});
app.listen(3000);
What each one does
| Middleware | Handles | Useful options |
|---|---|---|
express.json() | JSON bodies; fills req.body with an object. | limit, strict, type |
express.urlencoded() | Classic HTML form submissions. | extended (nested objects), limit |
express.static() | Serving files from a folder. | maxAge, index, dotfiles |
express.text() | Plain text bodies as a string. | type, limit |
express.raw() | Bodies as a Buffer, e.g. for verifying signed webhooks. | type, limit |
Each body parser only acts when the request's Content-Type header matches. A JSON parser skips a form post and vice versa, so registering both is safe.
Order matters more than you think
The most common bug with built-in middleware is registering a route before the parser. If app.post('/api/notes', ...) appears above app.use(express.json()), that route runs first and sees an empty body. Put parsers near the top of your app file.
A related surprise: when testing with a tool like Postman or curl, forgetting the Content-Type: application/json header means express.json() ignores the request and req.body is an empty object. The code is fine; the request is not.
For express.static(), paths are resolved relative to the folder where you started Node, not the file containing the code. Using path.join(__dirname, 'public') avoids broken asset links when the server is started from another directory. Static middleware placed before your routes also means a file named public/api would shadow an API path, so keep public folders free of names that clash with routes.
Bad input and size limits
When a client sends malformed JSON, express.json() passes a 400 error to your error-handling middleware rather than crashing. Add an error handler with four arguments, (err, req, res, next), to return a clean message. The limit option rejects oversized bodies with a 413 status, which protects the server from very large payloads.
The short version
Register express.json() and express.urlencoded() before your routes to fill req.body, serve assets with express.static() using absolute paths, set sensible size limits, and check the request's content type when the body looks empty.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.