***

title: "Middleware"
generated: true
---------------

<GenerationInfo sourceFile="packages/core/src/common/types/common-types.ts" sourceLine="228" packageName="@vendure/core" />

Defines API middleware, set in the [ApiOptions](/current/core/reference/typescript-api/configuration/api-options#apioptions). Middleware can be either
[Express middleware](https://expressjs.com/en/guide/using-middleware.html) or [NestJS middleware](https://docs.nestjs.com/middleware).

## Increasing the maximum request body size limit

Internally, Vendure relies on Express's built-in JSON parser (`express.json()`, which itself wraps
the body-parser library) to parse incoming JSON data. By default, the maximum body size is set to
100kb. Attempting to send a request with more than 100kb of JSON data will result in a
`PayloadTooLargeError`. To increase this limit, we can manually configure the middleware:

*Example*

```ts
import { VendureConfig } from '@vendure/core';
import { json } from 'express';

export const config: VendureConfig = {
  // ...
  apiOptions: {
    middleware: [{
      handler: json({ limit: '10mb' }),
      route: '*splat',
      beforeListen: true,
    }],
  },
};
```

The `route` may also be scoped to a single API path if you only want to raise the limit (or
otherwise customize body parsing) for that route, leaving the default parser in place everywhere
else:

*Example*

```ts
apiOptions: {
  middleware: [{
    handler: json({ limit: '10mb' }),
    route: '/admin-api',
    beforeListen: true,
  }],
},
```

```ts title="Signature"
interface Middleware {
    handler: MiddlewareHandler;
    route: string;
    beforeListen?: boolean;
}
```

<div className="members-wrapper">

### handler

\<MemberInfo kind="property" type={`MiddlewareHandler`}   />

The Express middleware function or NestJS `NestMiddleware` class.

### route

\<MemberInfo kind="property" type={`string`}   />

The route to which this middleware will apply. Pattern based routes are supported as well.

The `'ab*cd'` route path will match `abcd`, `ab_cd`, `abecd`, and so on. The characters `?`, `+`, `*`, and `()` may be used in a route path,
and are subsets of their regular expression counterparts. The hyphen (`-`) and the dot (`.`) are interpreted literally.

### beforeListen

\<MemberInfo kind="property" type={`boolean`} default={`false`}  since="1.1.0"  />

When set to `true`, this will cause the middleware to be applied before the Vendure server (and underlying Express server) starts listening
for connections. In practical terms this means that the middleware will be at the very start of the middleware stack, before even the
JSON body-parsing middleware which is automatically applied by Express. This can be useful in certain cases such as when you need to access the
raw unparsed request for a specific route.

</div>
