middleware/body

module:middleware/body

Description:
  • Parse body for JSON and x-www-form-urlencoded content.

    Store parsed data in the context.body.

    Default max body size is 64k, can be configued via middleware-body-max-size = NNNN config parameter, in bytes.

    Global node

    Enabled via middleware-body-enable = true no need to register the middleware in the code, it wil be enabled on startup automatically. Just check for context.body in the route handlers.

    Additional content types can be confgured via middleware-body-content-type = type1, type2, ...

    Manual mode see below:

    const { api, middleware } = require("backendjs");
    
    api.app.post("*", middleware.body)
    
    // or individually
    
    api.app.post("/api/data", middleware.body, (context, next) => {
        if (context.body?.id) ....
    })
    
    

    Additional content types can be collected by binding the parser with custom object containing supported properties:

    api.app.post("/data", middleware.body.handle.bind({
        contentType: ["mime/type1", "mime/type2"],
        maxSize: 100000,
        timeout: 3000
    }))
    
Source:

Methods

(static) configureMiddleware()

Description:
  • Start global middleware if enabled, run body parser for all endpoints

    Only methods in middleware-body-methods enabled, defaults are POST/PUT/PATCH.

Source:

(static) handle(context, next)

Source:
Parameters:
Name Type Description
context RequestContext
next function()
Example
api.app.post("*", middleware.body)

api.app.post("/api", middleware.body)