Express

Payload utilizes a few Express-specific middleware packages within its own routers. You can customize how they work by passing in configuration options to the main Payload config's express property.

Custom Middleware

Payload allows you to pass in custom Express middleware to be used on all of the routes it opens. This is useful for adding logging or any other custom functionality to your endpoints.

There are 2 exposed properties. Each property is an array of middleware functions.

  • preMiddleware - runs before any of the Payload middleware
  • postMiddleware - runs after all of the Payload middleware
1
{
2
express: {
3
preMiddleware: [
4
(req, res, next) => {
5
// do something
6
next()
7
}
8
],
9
postMiddleware: [
10
(req, res, next) => {
11
// do something
12
next()
13
}
14
]
15
}
16
}
17
18
// Example logging middleware function
19
const requestLoggerMiddleware = (req, res, next) => {
20
req.payload.logger.info(`request: ${req.method} ${req.url}`)
21
next()
22
}

JSON

express.json() is used to parse JSON body content into JavaScript objects accessible on the Express req. Payload allows you to customize all of the json method's options. Common examples of customization use-cases are increasing the max allowed JSON body size which defaults to 2MB.

Example payload.config.js for how to increase the max JSON size allowed to be sent to Payload endpoints:

1
{
2
express: {
3
json: {
4
limit: '4mb',
5
}
6
}
7
}

You can find a list of all available options that are able to be passed to express.json() here.

Compression

Payload uses the compression package to optimize transfer size for all of the routes it opens, and you can pass customization options through the Payload config.

To customize compression options, pass an object to the Payload config's express property.

Example payload.config.js:

1
{
2
express: {
3
compression: {
4
// settings go here
5
}
6
}
7
}

Typically, the default options for this package are suitable. However, for a list of all available customization options, click here.

Next

Database