The type option is used to determine what media type the middleware will parse. It is passed directly to the type-is library and this can be an extension name (like json), a mime type (like application/json), or a mime type with a wildcard (like */* or */json). Defaults to */*.
Basic usage
Lets assume that we have the following CURL command, which triggers POST /api/login endpoint:
Using previously connected bodyParser$ middleware, the app will intercept the following payload object:
The POST request body can be intercepted like follows.
All properties and values in req.body object are untrusted (unknown) and should be validated before trusting.
This middleware does not handle multipart bodies.
Advanced usage
The middleware does nothing if request Content-Type is not matched, which makes a possibility for chaining multiple parsers one after another. For example, we can register multiple middlewares that will parse only a certain set of possible Content-Type headers.
If the available parsers are not enough, you can create your own body parsers by conforming to RequestBodyParser interface. The example below shows how the jsonParser looks underneath.