Middleware not loading


#1

Morning,

I’m trying to test out the sample middleware before trying my own. I’m using the folder method as documented here: https://tyk.io/tyk-documentation/customise-tyk/javascript-middleware/install-middleware/install-middleware-tyk-pro/

The API Id I’m trying to get this to work for is 57c578dac5024002f0000001 (or at least I presume that’s the Id based on the dashboard URL)

So the file is in this location:

/opt/tyk-gateway/middleware/57c578dac5024002f0000001/pst/sampleMiddleware.js

Middleware path is set in tyk.conf as:

“middleware_path”: “/opt/tyk-gateway/middleware”,

Contents of the sampleMiddleware.js are:

// ---- Sample middleware creation by end-user -----
var sampleMiddleware = new TykJS.TykMiddleware.NewMiddleware({});

sampleMiddleware.NewProcessRequest(function(request, session) {
    // You can log to Tyk console output by calloing the built-in log() function:
    log("Running sample JSVM middleware")

    // Set and Delete headers in an outbound request
    request.SetHeaders["User-Agent"] = "Tyk-Custom-JSVM-Middleware";
    //request.DeleteHeaders.push("Authorization");

    // Change the outbound URL Path (only fragment, domain is fixed)
    // request.URL = "/get";

    // Add or delete request parmeters, these are encoded for the request as needed.
    request.AddParams["test_param"] = "My Teapot";
    request.DeleteParams.push("delete_me");

    // Override the body:
    request.Body = "New Request body"

    // If you have multiple middlewares that need to communicate, set or read keys in the ses$
    // This will only work in a postprocessing MW
    if (session.meta_data) {
        session.meta_data["MiddlewareDataString"] = "SomeValue";
    }

    // You MUST return both the request and session metadata
    return sampleMiddleware.ReturnData(request, session.meta_data);
});

// Ensure init with a post-declaration log message
log("Sample middleware initialised");

However this never seems to get loaded, or at least I never see any of the log messages.

Any advise or can you spot what I’m doing wrong?


#2

Can you check if the JSVM is enabled in your tyk.conf?


#3

"enable_jsvm": true

Sorry should have included that as well to begin with!


#4

I’ve figured one part of the problem out I think.

The documentation is wrong (or the code is wrong). Here’s the exert from the loading code:

	// Get POST folder path
	middlewarePostFolderPath := path.Join(config.MiddlewarePath, referenceSpec.APIDefinition.APIID, "post")
	mwPostFiles, _ := ioutil.ReadDir(middlewarePostFolderPath)

It’s expecting the directory name post however in the documentation it states the folder should be called pst:

https://tyk.io/tyk-documentation/customise-tyk/javascript-middleware/install-middleware/install-middleware-tyk-pro/

It still doesn’t look to be loading but I’ll keep digging. I’ll submit a documentation edit.


#5

Sorted. Final piece in the puzzle was I was using the Id of the API - not the api_id in the folder name!

Now works when placed in this location:

/opt/tyk-gateway/middleware/a83f9541eaf84f65616ecaab02e1bdff/post/sampleMiddleware.js

Thanks,
Richard