1. Home
  2. Tyk Open Source API Gateway v2.x
  3. Javascript Plugins
  4. Directory-based middleware loading

Directory-based middleware loading

In some cases middleware references can’t be directly embedded in API Definitions (for example, when using the dashboard). However this does not mean that there isn’t an easy way to distribute and enable custom middleware for an API on a Tyk node.

A second method of loading API Definitions in Tyk nodes is to add them as a directory structure in the Tyk node, Tyk will load the middleware plugins dynamically on host-reload without needing a direct reference to them in the API Definition.

The URL structure looks like this:

---- {APIID}
-------- pre/
------------ middlewareObject1Name.js
------------ middlewareObjcet2Name.js
-------- pst/
------------ middlewareObject1Name_with_session.js
------------ middlewareObjcet2Name.js

Tyk will check for a folder that matches the API ID being loaded, and then load the pre and post middleware from the respective folders. The filename must match the object to be loaded exactly.

If your middleware requires session injection, then append _with_session to the filename.