Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Files

Latest commit

 

History

History
46 lines (32 loc) · 2.89 KB

Middleware.md

File metadata and controls

46 lines (32 loc) · 2.89 KB

The Middleware class

The Middleware class is the main class in Swagger Express Middleware. It's role is simple: You give it a Swagger API, and it gives you Express middleware for that API. You can create multiple Middleware instances if you need to work with more than one Swagger API. Each Middleware instance is entirely isolated, so any Express middleware that is created by one instance will only know about its own Swagger API.

TIP: For most simple apps, you don't need to worry about the Middleware class. The createMiddleware function — which is used in all the documentation examples — is a convenience function that automatically instantiates a Middleware object and calls its init() method for you.

Constructor

Middleware(router)

This is the constructor for the Middleware class.

  • router (optional) - express.App or express.Router
    An Express Application or Router that will be used to determine settings (such as case-sensitivity and strict routing) and to register path-parsing middleware.

    NOTE: If you don't specify this parameter, then the default Express routing settings will be used (case-insensitive, non-strict). You can override this parameter (or the defaults) for any specific middleware by passing an Express App or Router to the middleware.

Methods

init(swagger, callback)

Initializes the middleware with the given Swagger API. This method can be called again to re-initialize with a new or modified API.

  • swagger (optional) - string or object
    The file path or URL of a Swagger 2.0 API spec, in YAML or JSON format. Or a valid Swagger object. Any $ref pointers to other files/URLs will be interpreted as relative to the main Swagger file.

  • callback (optional) - function(err, middleware)
    A callback function that will be called once the Swagger API is fully parsed, dereferenced, and validated. The second parameter is the same Middleware object.

files(router, options)

This method creates a new Files middleware instance.

metadata(router)

This method creates a new Metadata middleware instance.

CORS(router)

This method creates a new CORS middleware instance.

parseRequest(router, options)

This method creates a new Parse Request middleware instance.

validateRequest(router)

This method creates a new Validate Request middleware instance.

mock(router, dataStore)

This method creates a new Mock middleware instance.