Unable to call simple String functions in Middleware


I’ve got a pre custom middleware installed and it is getting hit before the endpoint is called. However, I can’t even run simple string functions.

var url = request.URL;
    var path = url.substr(url.indexOf('/',2) + 1, url.length);

The first three lines execute. But the last line fails. I tried startsWith too. What is going on?

tyk-gateway_1 | time="Jan 13 05:05:46" level=error msg="Failed to run JS middleware" api_id=venues123 api_name="Tyk Test Keyless API" error="TypeError: 'contains' is not a function" mw=DynamicMiddleware org_id=default123 origin= path="/serviceB/venues/15"

Also, if I wanted to use a library like moment or underscore how do I add them?

The last one fails because JS plugins run in a Sandboxed JSVM that is compiled in the Gateway binary. Anything other than the built in functions and objects would not work .

You can try to find a minified moment or underscore file, and then paste the Javascript into your JS CODE plugin, which is essentially “installing” a package. Then you could potentially call those functions. I tried it with moment JS and it worked.

Alternatively you could use Node with gRPC, then you can use any plugin.