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 aMiddleware
object and calls itsinit()
method for you.
Constructor
Middleware(router)
This is the constructor for the Middleware class.
- router (optional) -
express.App
orexpress.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
orobject
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 sameMiddleware
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.