KrakenJS config middleware and Express 4 router middleware execution priority
0 reputation · 18 Feb 2020, 16:12 UTC
0 reputation · 18 Feb 2020, 16:12 UTC
Determine whether middleware declared in KrakenJS 2.x config/middleware.json always executes before Express 4 router-level middleware registered inside controllers, and whether environment-specific config overrides can alter that relative execution order.
KrakenJS registers config middleware through app.use() before mounting application routes. Controller-level middleware is attached to Express routers after config registration, but KrakenJS does not document a supported API to reorder or remove config-declared middleware at runtime. Merge behavior for overlapping middleware keys between base and environment-specific configs is version-sensitive and undocumented.
Does KrakenJS 2.x guarantee that all config-declared middleware runs before any router-level middleware, regardless of environment-specific middleware overrides? When the base and environment configs define the same middleware key, does the final load order follow the base array, the override array, or an unspecified merge?
In KrakenJS 2.x, middleware declared in config/middleware.json is guaranteed to execute before any router-level middleware registered inside controllers. This is because KrakenJS follows a strict boot sequence where global configuration middleware is registered via app.use() before the application mounts the controllers and their respective Express 4 routers.
Environment-specific configuration overrides (e.g., middleware.dev.json) do not alter the relative execution order between the config layer and the router layer. They only change which middleware is registered during the config phase. The sequence remains: Config Middleware → Router Middleware.
When a middleware key exists in both the base middleware.json and an environment-specific override file, KrakenJS performs a replacement rather than a deep merge. If the environment config defines the same key, the entire value (or array) associated with that key in the base config is discarded and replaced by the override value.
To verify the execution order and override behavior in your specific environment, you can use the following methods:
console.log('Config Middleware') in a global middleware and a console.log('Router Middleware') in a controller's router. The config log will always appear first in the terminal.// Inside a controller or startup script
console.log(app._router.stack.map(layer => layer.handle.name));
This behavior assumes a standard KrakenJS 2.x installation using Express 4. It assumes that no custom boot-strapping logic has been added to the app.js or internal framework files to manually reorder the app.use() calls. Because KrakenJS does not provide a public API to reorder middleware once the boot sequence has started, the order is effectively immutable at runtime.
Diagnostic Detail Needed: Are you using any custom app.js modifications or third-party boot-sequence plugins that intercept the config/middleware.json loading process?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.