DEV Community

Cover image for Understanding `app.use()` in Express.js: The Backbone of Middleware
Srishti Prasad
Srishti Prasad

Posted on

Understanding `app.use()` in Express.js: The Backbone of Middleware

If you've started learning Node.js and Express.js, you've probably seen app.use() in almost every project:

const express = require("express");
const app = express();

app.use(express.json());
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

At first glance, it may seem like just another Express function. But in reality, app.use() is one of the most important concepts in Express. It is responsible for building the request-processing pipeline by registering middleware.

In this article, we'll explore what app.use() is, why it's important, how it works internally, and common real-world use cases.


What is app.use()?

app.use() is a method provided by Express that registers middleware.

Middleware is simply a function that executes between receiving an HTTP request and sending the HTTP response.

Every incoming request passes through one or more middleware functions before it reaches the final route handler.

The syntax is:

app.use([path], middleware);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini
  • path (optional): URL path where the middleware should run.
  • middleware: Function executed for matching requests.

A middleware function looks like this:

function middleware(req, res, next) {
    console.log("Middleware executed");
    next();
}
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

The next() function tells Express to continue processing the next middleware or route handler.


Why Do We Need Middleware?

Imagine an Express application without middleware.

app.get("/users", (req, res) => {
    res.send("Users");
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

The request directly reaches the route handler.

But real-world applications need much more:

  • Validate incoming requests
  • Parse JSON
  • Authenticate users
  • Log requests
  • Handle errors
  • Enable CORS
  • Serve static files

Instead of repeating this logic in every route, Express lets us write reusable middleware and register it using app.use().


How Express Processes Requests

Suppose we have:

app.use((req, res, next) => {
    console.log("Middleware 1");
    next();
});

app.use((req, res, next) => {
    console.log("Middleware 2");
    next();
});

app.get("/", (req, res) => {
    console.log("Route Handler");
    res.send("Hello");
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

When a request comes to /, Express executes the following pipeline:

Incoming Request
       โ”‚
       โ–ผ
Middleware 1
       โ”‚
       โ–ผ
Middleware 2
       โ”‚
       โ–ผ
Route Handler
       โ”‚
       โ–ผ
Response Sent
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Console Output:

Middleware 1
Middleware 2
Route Handler
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Express simply walks through its middleware stack in the order the middleware was registered.


The Importance of next()

Every middleware receives three parameters:

(req, res, next)
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Example:

app.use((req, res, next) => {
    console.log("Request received");
    next();
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

next() tells Express:

"I'm done. Move to the next middleware."

If you forget to call next(), Express stops processing.

Example:

app.use((req, res, next) => {
    console.log("Stopped here");
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Now every request hangs because Express never moves to the next middleware.


Global Middleware

Without specifying a path, middleware runs for every request.

app.use((req, res, next) => {
    console.log(req.method, req.url);
    next();
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Requests:

GET /users
POST /login
DELETE /products/5
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

All three execute this middleware.

This is why logging middleware is usually global.


Path-Specific Middleware

Middleware can also be attached to a specific path.

app.use("/api", (req, res, next) => {
    console.log("API Middleware");
    next();
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

This middleware executes for:

GET /api/users
POST /api/login
PUT /api/orders
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

But not for:

GET /
GET /about
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Express matches the URL prefix before executing the middleware.


app.use() Works for All HTTP Methods

Unlike app.get() or app.post(), app.use() doesn't care about the HTTP method.

app.use("/users", middleware);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Runs for:

GET /users
POST /users
PUT /users
DELETE /users
PATCH /users
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

This makes it perfect for middleware like authentication.


Real-World Use Cases

1. Parsing JSON

app.use(express.json());
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Without this:

console.log(req.body);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Output:

undefined
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

With express.json():

{
  "name": "John"
}
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

becomes

req.body = {
  name: "John"
};
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

2. Logging Requests

app.use((req, res, next) => {
    console.log(`${req.method} ${req.url}`);
    next();
});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Output:

GET /users
POST /login
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

3. Authentication

app.use((req, res, next) => {

    if (!req.headers.authorization) {
        return res.status(401).send("Unauthorized");
    }

    next();

});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Every request must pass authentication before reaching the routes.


4. CORS

const cors = require("cors");

app.use(cors());
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

This allows browsers to make requests from different origins.


5. Serving Static Files

app.use(express.static("public"));
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Now:

public/logo.png
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

is available as:

http://localhost:3000/logo.png
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

6. Mounting Routers

Suppose we have:

// users.js

router.get("/", getUsers);

router.post("/", createUser);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Register it:

app.use("/users", userRouter);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Routes become:

GET /users
POST /users
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

This keeps applications modular and easier to maintain.


Execution Order Matters

Consider:

app.use(authMiddleware);

app.get("/dashboard", dashboard);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Every request to /dashboard is authenticated.

Now reverse them:

app.get("/dashboard", dashboard);

app.use(authMiddleware);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Now authentication never runs because the response is already sent.

Middleware executes strictly in the order it is registered.


Error-Handling Middleware

Express recognizes error middleware by four parameters.

app.use((err, req, res, next) => {

    console.error(err);

    res.status(500).json({
        message: err.message
    });

});
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Whenever next(error) is called, Express skips normal middleware and jumps directly to this error handler.


app.use() vs app.get()

Feature app.use() app.get()
Purpose Register middleware Register a GET route
HTTP Methods All methods Only GET
Path Required Optional Required
Calls next() Yes Usually sends response
Used For Logging, auth, parsing, routers Handling GET requests

Example:

app.use("/users", middleware);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Handles:

GET /users
POST /users
PUT /users
DELETE /users
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Whereas:

app.get("/users", handler);
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Handles only:

GET /users
๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini ๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini

Best Practices

  • Register middleware in the correct order.
  • Always call next() unless you're sending a response.
  • Keep middleware focused on a single responsibility.
  • Use path-specific middleware when appropriate.
  • Place error-handling middleware at the end of your middleware stack.

Conclusion

app.use() is the foundation of an Express application. It lets you build a request-processing pipeline where each middleware performs a specific task before handing control to the next one.

Whether you're parsing request bodies, authenticating users, logging requests, enabling CORS, serving static files, or organizing routes, app.use() is the mechanism that ties everything together.

Understanding how app.use() worksโ€”and especially how middleware order and next() affect request flowโ€”is essential for writing clean, scalable, and maintainable Express applications.

If you're learning Express, mastering app.use() is one of the biggest steps toward understanding how Express handles requests under the hood.


Thanks for reading! ๐Ÿš€ If you found this article helpful, consider sharing it with fellow developers who are learning Express.js.

Top comments (2)

๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini
 
nazar-boyko profile image
Nazar Boyko •

There's a gotcha worth adding to the error handler section. Express decides a function is an error handler purely by its number of parameters, so it needs all four even when next goes unused. If a linter or a tidy teammate strips that unused next, the function silently turns into regular middleware and errors start falling through, with no warning anywhere. It bites people precisely because everything still appears to run. The reversed dashboard example is a nice touch, ordering is the thing beginners hit first and your version makes it visible.

๐Ÿ“บ Info Siaran: srishtikprasad/understanding appuse in expressjs the backbone of middleware mg - Pertandingan Hari Ini
 
frank_signorini profile image
Frank •

How does app.use() handle middleware functions with multiple arguments, I've seen some examples but still unsure. Would love to hear your thoughts on this.