Middleware
Middleware
Defines API middleware, set in the ApiOptions. Middleware can be either Express middleware or NestJS middleware.
Increasing the maximum request body size limit
Internally, Vendure relies on the body-parser middleware 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 body-parser middleware:
Example
import { VendureConfig } from '@vendure/core';
import { json } from 'body-parser';
export const config: VendureConfig = {
// ...
apiOptions: {
middleware: [{
handler: json({ limit: '10mb' }),
route: '*',
beforeListen: true,
}],
},
};
interface Middleware {
handler: MiddlewareHandler;
route: string;
beforeListen?: boolean;
}
handler
MiddlewareHandler
The Express middleware function or NestJS NestMiddleware
class.
route
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
boolean
false
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
body-parser
middleware which is automatically applied by NestJS. This can be useful in certain cases such as when you need to access the
raw unparsed request for a specific route.