Middleware Tutorial


I have setup the docker Tyk environment and Im about to write some javascript middleware. I have a few basic questions:

  • Is the only way to insert the middleware settings, via mongodb? Is there any way to do this in the GUI?
  • I read that the TykHttpRequest executes synchronously. Our use case is to push out some analytics data for each call via the middleware, so is there any way to do this so that it doesn’t block the main request to the proxied API service?
  • Is there any way to test a js middleware script in isolation without having to load it into the tyk gateway
  • In terms of the development cycle, do I need to restart the gateway after each change to the javascript middleware? (Really, I suppose Im asking, more generally, what is the best and most convenient way to develop such middleware…)

Thanks for your help


You can use directory-based middleware loading to make it easier with pro deployments. It means you just edit the file and reload instead of messing with Mongo.

There isn’t a way to handle JS middleware via the GUI. That’s what the directory loser is for.

Afraid not, the method that handles these requests is blocking though I can see the case for adding an asyn Fire-and-forget version. I’d suggest looking at Tyk Pump custom analytics purgers to handle pushing analytics data to different data sinks.

We use a JS environment called Otto that provides the interpreter, you can compile and install it to have a CLI version that executes your JS, though you won’t have the JSVM API custom to Tyk obviously

I think a hot reload will do (save an API or send the API call), it will cause all the JSVM environments to be recreated.

You could test you initial code using the otto interpreter and then the custom Tyk calls with a combo of directory-based loading and hot reloads to test.

Hope that helps :slight_smile: