---
title: "Middleware"
url: https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Middleware

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/java.md), [Swift](https://docs.zeroc.com/ice/3.8/runtime/dispatch/middleware/swift.md).

A *middleware* is a [dispatcher](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatcher-api/js.md) that intercepts an incoming request before it reaches the servant, and the outgoing response before the object adapter sends it back. A middleware holds the next dispatcher of the [dispatch pipeline](https://docs.zeroc.com/ice/3.8/runtime/dispatch/dispatch-pipeline.md): its dispatch method can run code before and after calling this next dispatcher, or return a response itself, such as an error, without calling it.

## Installing a Middleware

[`use`](https://code.zeroc.com/ice/3.8/api/javascript/Ice/ObjectAdapter.html#use) installs a middleware on an object adapter. It takes a factory that creates the middleware from the next dispatcher, and the object adapter calls this factory when it dispatches its first request. Install all the middleware of an object adapter before this first dispatch. They intercept requests in the order you install them.

For example, the following middleware takes the next dispatcher and a token, and the call to `use` installs it:

```js
class AuthorizationMiddleware extends Ice.Object
{
    async dispatch(request) {
        // ...
    }

    constructor(next, validToken) {
        // ...
    }
}

adapter.use(next => new AuthorizationMiddleware(next, "iced tea"));
```

```typescript
class AuthorizationMiddleware extends Ice.Object
{
    override async dispatch(
        request: Ice.IncomingRequest): Promise<Ice.OutgoingResponse> {
        // ...
    }

    constructor(next: Ice.Object, validToken: string) {
        // ...
    }
}

adapter.use((next: Ice.Object) => new AuthorizationMiddleware(next, "iced tea"));
```

## Built-in Middleware

Ice installs a logger middleware at the beginning of the dispatch pipeline, unless both `Ice.Warn.Dispatch` and `Ice.Trace.Dispatch` are set to `0`.
