Getting javascript plugins to run in Hybrid environment

I’m trying to get a sample JS middleware plugin to run on my hybrid environment.
I’ve followed the instructions https://tyk.io/docs/customise-tyk/plugins/javascript-middleware/install-middleware/tyk-hybrid/ but I can’t get it to work.

my tyk. conf is as below:
[…]
“enable_jsvm”: true,
“oauth_redirect_uri_separator”: “;”,

“coprocess_options”: {
“enable_coprocess”: true,
“python_path_prefix”: “/opt/tyk-gateway”
},
“enable_bundle_downloader”: true,
“bundle_base_url”: “http://localhost:8000/”,
“public_key_path”: “/path/to/my/pubkey”,
[…]

my middleware folder contains:
API
–/pre
-----/samplePreProcessMiddleware.js

I consistently get the following message : ERROR jsvm: Failed to run JS middleware: ReferenceError: ‘samplePreProcessMiddleware’ is not defined

If I change the filename, the error comes up with the matching name.
If I change the name of the folder, the gateway doesn’t pick up the plugin, the API respond fine (without plugin)

Any help much appreciated !

Your middleware should have JS middleware object with name samplePreProcessMiddleware.

Can you share your middleware?

Sure, it’s the sample one provided within the install

Pls also share the full logs when gateway loads your API.

Thank you.

Ah, one more thing. Pls enable verbose logging by setting TYK_LOGLEVEL=debug variable to hybrid container (you can modify start.sh file to do that).

Here they are


Hi Stephane, can you share your API definition?

Hi Matias,

This is the API definition

{
“api_model”: {},
“api_definition”: {
“api_id”: “e7b615f7fe81483760396ae8ad7e3d52”,
“upstream_certificates”: {},
“use_keyless”: true,
“enable_coprocess_auth”: false,
“jwt_disable_issued_at_validation”: false,
“custom_middleware”: {
“pre”: [],
“post”: [],
“post_key_auth”: [],
“auth_check”: {
“name”: “”,
“path”: “”,
“require_session”: false
},
“response”: [],
“driver”: “”,
“id_extractor”: {
“extract_from”: “”,
“extract_with”: “”,
“extractor_config”: {}
}
},
“disable_quota”: false,
“custom_middleware_bundle”: “”,
“cache_options”: {
“cache_timeout”: 0,
“enable_cache”: false,
“cache_all_safe_requests”: false,
“cache_response_codes”: [],
“enable_upstream_cache_control”: false,
“cache_control_ttl_header”: “”
},
“enable_ip_blacklisting”: false,
“tag_headers”: [],
“pinned_public_keys”: {},
“expire_analytics_after”: 0,
“domain”: “”,
“openid_options”: {
“providers”: [],
“segregate_by_client”: false
},
“active”: true,
“config_data”: {},
“notifications”: {
“shared_secret”: “”,
“oauth_on_keychange_url”: “”
},
“auth”: {
“auth_header_name”: “”,
“use_certificate”: false
},
“check_host_against_uptime_tests”: false,
“auth_provider”: {
“name”: “”,
“storage_engine”: “”,
“meta”: {}
},
“blacklisted_ips”: [],
“hmac_allowed_clock_skew”: -1,
“dont_set_quota_on_create”: false,
“uptime_tests”: {
“check_list”: [],
“config”: {
“expire_utime_after”: 0,
“service_discovery”: {
“use_discovery_service”: false,
“query_endpoint”: “”,
“use_nested_query”: false,
“parent_data_path”: “”,
“data_path”: “”,
“cache_timeout”: 60
},
“recheck_wait”: 0
}
},
“enable_jwt”: false,
“do_not_track”: false,
“name”: “bakery”,
“jwt_disable_expires_at_validation”: false,
“slug”: “bakery”,
“oauth_meta”: {
“allowed_access_types”: [],
“allowed_authorize_types”: [],
“auth_login_redirect”: “”
},
“CORS”: {
“enable”: false,
“max_age”: 0,
“allow_credentials”: false,
“exposed_headers”: [],
“allowed_headers”: [],
“options_passthrough”: false,
“debug”: false,
“allowed_origins”: [],
“allowed_methods”: []
},
“event_handlers”: {
“events”: {}
},
“proxy”: {
“target_url”: “http://betclicmockup.azurewebsites.net/”,
“endpoint_returns_list”: false,
“cache_timeout”: 0,
“parent_data_path”: “”,
“service_discovery”: {
“endpoint_returns_list”: false,
“cache_timeout”: 0,
“parent_data_path”: “”,
“query_endpoint”: “”,
“use_discovery_service”: false,
“target_path”: “”,
“use_target_list”: false,
“use_nested_query”: false,
“data_path”: “”,
“port_data_path”: “”
},
“check_host_against_uptime_tests”: false,
“transport”: {
“ssl_ciphers”: [],
“ssl_min_version”: 0,
“proxy_url”: “”
},
“target_list”: [],
“query_endpoint”: “”,
“use_discovery_service”: false,
“_sd_show_port_path”: false,
“preserve_host_header”: false,
“use_target_list”: false,
“strip_listen_path”: true,
“use_nested_query”: false,
“data_path”: “”,
“port_data_path”: “”,
“enable_load_balancing”: false,
“listen_path”: “/e7b615f7fe81483760396ae8ad7e3d52/”
},
“client_certificates”: [],
“use_basic_auth”: false,
“version_data”: {
“not_versioned”: true,
“default_version”: “”,
“versions”: {
“Default”: {
“name”: “Default”,
“expires”: “”,
“paths”: {
“ignored”: [],
“white_list”: [],
“black_list”: []
},
“use_extended_paths”: true,
“global_headers”: {
“x-functions-key”: “nRmhniVGpWy1FTZ7d5zswj6zoNHVs8danosKu4raH0WpErllH1YfFQ==”
},
“global_headers_remove”: [],
“global_size_limit”: 0,
“override_target”: “”,
“extended_paths”: {
“white_list”: [
{
“path”: “/api/pastries/{pastry}”,
“method_actions”: {
“GET”: {
“action”: “no_action”,
“code”: 200,
“headers”: {}
}
}
}
],
“url_rewrites”: [
{
“path”: “/api/pastries/{pastry}”,
“method”: “GET”,
“match_pattern”: “api/pastries/(.*)”,
“rewrite_to”: “api/books/$1”,
“triggers”: [],
“MatchRegexp”: null
}
]
}
}
}
},
“use_standard_auth”: false,
“session_lifetime”: 0,
“disable_rate_limit”: false,
“definition”: {
“location”: “header”,
“key”: “x-api-version”,
“strip_path”: false
},
“use_oauth2”: false,
“allowed_ips”: [],
“org_id”: “5b59da66276db400011375f0”,
“enable_ip_whitelisting”: false,
“global_rate_limit”: {
“rate”: 0,
“per”: 0
},
“enable_context_vars”: false,
“tags”: [],
“basic_auth”: {
“disable_caching”: false,
“cache_ttl”: 0
},
“session_provider”: {
“name”: “”,
“storage_engine”: “”,
“meta”: {}
},
“strip_auth_data”: false,
“id”: “5b7ae093bebdbb0001d65325”,
“enable_signature_checking”: false,
“use_openid”: false,
“jwt_skip_kid”: false,
“enable_batch_request_support”: false,
“jwt_disable_not_before_validation”: false,
“response_processors”: [],
“use_mutual_tls_auth”: false
},
“hook_references”: [],
“is_site”: false,
“sort_by”: 0
}

Looks good however your custom_middleware block looks empty. In any case try using a block like this (make sure the driver flag is present):

    "custom_middleware": {
        "driver": "otto",
        "pre": [
            {
                "name": "sampleMiddleware",
                "path": "middleware/sampleMiddleware.js"
            }
        ]
    }