Request middleware process

Imported Google Group message. Original thread at: Redirecting to Google Groups Import Date: 2016-01-19 21:08:34 +0000.
Sender:Tarmo Janson.
Date:Tuesday, 24 March 2015 08:54:27 UTC.

I have a sample test API and a simple middleware that should print to the console, but if I make a request then the middleware doesn’t process it and nothing is printed to the console.

The API definition includes:
“proxy”:{
“listen_path”:“/test/”,
“target_url”:“http://httpbin.org/”,
“strip_listen_path”:true
},“custom_middleware”:{
“pre”:[

],
"post":[  
    {
        "name": "sampleMiddleware",
        "path": "/etc/tyk/middleware/sampleMiddleware.js",
        "require_session": true
    }
]

}

The middleware:
log(“Sample middleware started”);

var sampleMiddleware = new TykJS.TykMiddleware.NewMiddleware({});

sampleMiddleware.NewProcessRequest(function(request, session) {
log(“Running sample JSVM middleware”);
return sampleMiddleware.ReturnData(request, session.meta_data);
});

log(“Sample middleware initialised”);

which at tyk startup prints the following to the console:
INFO[0000] JSVM LOG: Sample middleware started
INFO[0000] JSVM LOG: Sample middleware initialised

Making a request to
http://33.33.33.240:5000/test/

What could be the problem here?

Imported Google Group message.
Sender:Martin Buhr.
Date:Tuesday, 24 March 2015 09:58:05 UTC.

Hi,

Is the API definition using Keyless access? Unfortunately we haven’t enabled custom middleware on Keyless API’s, they are pretty limited in terms of middleware mainly because of a lack of session. You could try swapping to a token-based method and adding a key (either with the dashboard or with the REST API, make sure you enable access for the key to the API ID, here’s a curl example:

curl http://HOST:PORT/tyk/keys/create
–header “x-tyk-authorization: ”
–data ‘{
“allowance”: 1000,
“rate”: 1000,
“per”: 60,
“expires”: 0,
“quota_max”: -1,
“quota_renews”: 1406121006,
“quota_remaining”: 0,
“quota_renewal_rate”: 60,
“access_rights”: {
“”: {
“api_name”: “My Test API”,
“api_id”: “”,
“versions”: [
“Default”
]
}
},
“org_id”: “”
}’

I just tried the middleware code you posted with our development environment and the middleware executes with a token-based API Definition.

Let me know if that works :slight_smile:

Thanks,
Martin

  • show quoted text -

Imported Google Group message.
Sender:Tarmo Janson.
Date:Tuesday, 24 March 2015 09:22:05 UTC.

Hi,

Can you post your tyk.conf? I’m asking because the classes that define that interface are stored in a file called tyk.js and will be pointed to in the tyk.conf by the field:

“tyk_js_path”: “/etc/tyk/js/tyk.js”,

If Tyk can’t find that file, it will initialise all the built-in VM functions but not the base classes taht make up the middleware interface. I’d suggest confirming if that file exists, and secondly if the path is correct. The file should come bundled with the application, but we’ve found some instances where it might be missing. If it’s missing, you can download it from GH here:

https://raw.githubusercontent.com/lonelycode/tyk/master/js/tyk.js

Let me know how it goes :slight_smile:

Thanks,
Martin

  • show quoted text -

Imported Google Group message.
Sender:Martin Buhr.
Date:Tuesday, 24 March 2015 09:29:58 UTC.

tyk.conf:
{
“listen_port”: 5000,
“secret”: “352d20ee67be67f6340b4c0605b044b7”,
“template_path”: “/etc/tyk/templates”,
“use_db_app_configs”: false,
“app_path”: “/etc/tyk/apps/”,
“tyk_js_path”: “/etc/tyk/js/tyk.js”,
“middleware_path”: “/etc/tyk/middleware”,
“storage”: {
“type”: “redis”,
“host”: “localhost”,
“port”: 6379,
“username”: “”,
“password”: “”
},
“enable_analytics”: true,
“analytics_config”: {
“type”: “mongo”,
“csv_dir”: “/tmp”,
“mongo_url”: “mongodb://localhost/tyk_analytics”,
“mongo_db_name”: “tyk_analytics”,
“mongo_collection”: “tyk_analytics”,
“purge_delay”: 10,
“ignored_ips”: []
},
“health_check”: {
“enable_health_checks”: true,
“health_check_value_timeouts”: 60
}
}

And the the same tyk.js file is there already.

  • show quoted text -

Imported Google Group message.
Sender:Tarmo Janson.
Date:Tuesday, 24 March 2015 09:58:05 UTC.

Hi,

Is the API definition using Keyless access? Unfortunately we haven’t enabled custom middleware on Keyless API’s, they are pretty limited in terms of middleware mainly because of a lack of session. You could try swapping to a token-based method and adding a key (either with the dashboard or with the REST API, make sure you enable access for the key to the API ID, here’s a curl example:

curl http://HOST:PORT/tyk/keys/create
–header “x-tyk-authorization: ”
–data ‘{
“allowance”: 1000,
“rate”: 1000,
“per”: 60,
“expires”: 0,
“quota_max”: -1,
“quota_renews”: 1406121006,
“quota_remaining”: 0,
“quota_renewal_rate”: 60,
“access_rights”: {
“”: {
“api_name”: “My Test API”,
“api_id”: “”,
“versions”: [
“Default”
]
}
},
“org_id”: “”
}’

I just tried the middleware code you posted with our development environment and the middleware executes with a token-based API Definition.

Let me know if that works :slight_smile:

Thanks,
Martin

  • show quoted text -

Imported Google Group message.
Sender:Martin Buhr.
Date:Tuesday, 24 March 2015 12:19:16 UTC.

Thanks,

It works with Basic Auth.

  • show quoted text -