KrakenJS Routing Layer and HTTP Server Boot Sequence Interoperability
0 reputation · 16 Dec 2020, 01:29 UTC
0 reputation · 16 Dec 2020, 01:29 UTC
Goal: Verify whether KrakenJS guarantees that all routes and services are fully registered before the underlying HTTP server starts accepting connections.
During the framework’s boot sequence, the Express application is assembled and the HTTP server is bound to a port, but the automatic discovery of controllers and services can involve asynchronous initialization (e.g., loading configuration files or plugin modules). This creates uncertainty about the exact point at which the server begins listening relative to the completion of route registration.
server.listen (or equivalent) relative to the completion of the controller‑loading phase?34775 reputation · 16 Dec 2020, 10:25 UTC
KrakenJS does guarantee that all automatically discovered controllers and routes are registered before the HTTP server begins accepting connections. The framework's bootstrapper executes route registration synchronously during the boot phase and only invokes server.listen() after that phase completes without error.
require of config/ files)app.use()/app.METHOD() calls)'boot' event emissionserver.listen(port, callback) invocationSteps 1–3 run on the same tick; step 5 occurs only after step 4's listeners return.
Listen for the 'boot' event on the Kraken application instance:
const kraken = require('kraken-js');
const app = kraken.create();
app.on('boot', function () {
// All auto-discovered routes are now registered
console.log('Routes registered, safe to start custom async work');
});
app.listen(8000); // server.listen called internally after 'boot' fires
This event is the documented signal that the Express router is fully populated.
The guarantee covers framework-managed discovery only. If a service constructor or a controller's initialize() method starts asynchronous work (database connections, external API calls, file I/O) without returning a promise that the bootstrapper awaits, that work may still be in flight when server.listen fires. KrakenJS does not automatically await arbitrary promises returned from service constructors.
Documented behavior: requests arriving during that window will reach Express middleware and route handlers, but any handler that depends on the unfinished async initialization may crash or return 500. The framework makes no "graceful degradation" promise for this scenario.
console.log('boot event') in an app.on('boot', …) listener.console.log('controller loaded') in a controller file's top-level code.Are you using any service or controller that performs asynchronous initialization without returning a promise to the bootstrapper (e.g., setTimeout, bare Promise constructor, event-emitter callbacks)? If yes, the race window exists; if no, the guarantee holds.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.