Automatic Middleware Registration in KrakenJS: Simplifying Express Setup
Learn how KrakenJS automatically loads middleware from the middleware directory, reducing boilerplate and ensuring predictable ordering, with a practical rate‑limiting example.
22 Oct 2025, 06:38 UTC

Problem: Boilerplate Middleware Setup
When building an Express application with KrakenJS, developers often repeat the same app.use() calls for concerns such as compression, CSRF protection, security headers, and request logging. This repetition clutters index.js and makes it easy to forget a middleware or to load them in the wrong order.
How KrakenJS Auto‑loads Middleware
KrakenJS follows a convention‑over‑configuration approach: any file placed in the middleware/ directory that exports a function with the signature (req, res, next) is automatically appended to the Express middleware stack. The loading happens during application startup, and the files are processed in lexical (alphabetical) order.
During start‑up KrakenJS logs a line like Loading middleware: compression.js for each file, giving you visibility into the order.
Worked Example: Adding a Rate‑Limiter
- Create a file
middleware/ratelimit.jswith the following content: - Restart the KrakenJS server (
npm startor whatever start script you use). - Observe the startup log; you should see a line similar to
Loading middleware: ratelimit.js. - Make a request to any route and verify that the
RateLimit-Remainingheader is present and decrements as expected.
// middleware/ratelimit.js
const rateLimit = require('express-rate-limit');
module.exports = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per window
standardHeaders: true,
legacyHeaders: false,
});
Because the file name starts with “r”, it will be loaded after any files that begin with letters earlier in the alphabet (e.g., 01‑compression.js, 02‑csurf.js). If you need the limiter to run before compression, rename the file to something like 00‑ratelimit.js.
Trade‑off and Limitation
The automatic ordering is purely lexical. Developers must prefix files with numbers or carefully chosen names to control precedence. This works well for global concerns but does not allow skipping a middleware for a specific route without extra logic; route‑specific middleware still needs to be added manually via app.use('/path', ...) or a KrakenJS router.
Additionally, the feature relies on KrakenJS 2.x, which targets Express 4. Moving to Express 5 may break the auto‑loading convention until a compatible KrakenJS release is published.
Actionable Closing
To take advantage of auto‑loading:
- Keep the
middleware/directory for cross‑cutting concerns that should run on every request. - Use numeric prefixes (e.g.,
01‑,02‑) to make the order explicit and avoid surprises. - Verify the load order by checking the startup logs or inspecting
app._router.stackafter the application starts. - For middleware that should apply only to a subset of routes, continue to register them manually or mount them on a dedicated router.
By following these conventions you reduce boilerplate, keep middleware ordering predictable, and maintain a clean index.js.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.