Understanding Web Framework Architectural Patterns: Middleware, MVC, and Dependency Injection
This article explores the philosophical limits of design patterns and then dives into three core backend architectural patterns—Middleware, MVC, and Dependency Injection—illustrating their purpose, implementation in Node.js frameworks like Express and Koa, and their impact on code modularity and scalability.
The article begins with a philosophical discussion on the limits of scientific induction and design patterns, arguing that patterns are context‑specific solutions rather than absolute truths, and that their value lies in communication and abstraction.
It then introduces three essential backend architectural patterns—Middleware, MVC, and Dependency Injection—explaining why they remain relevant in modern web development.
Middleware Pattern
In a typical Node.js request‑response cycle, various concerns such as logging, authentication, parsing, routing, error handling, and timing need to be processed. The middleware approach centralises control flow, decouples processing modules, and enables declarative, configurable services.
The classic Intercepting Filter pattern from J2EE is shown, followed by an equivalent Express/Koa implementation.
public class DebuggingFilter implements Processor {
private Processor target;
public DebuggingFilter(Processor myTarget) {
target = myTarget;
}
public void execute(ServletRequest req, ServletResponse res) throws IOException, ServletException {
// preprocess
target.execute(req, res);
// post‑process
}
}Express middleware is introduced with the familiar signature and a simple example:
var express = require('express');
var app = express();A more complex router example demonstrates how Express 4.x can organise large applications:
/* file bird.js */
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('Birds home page');
});
module.exports = router;
/* file app.js */
var birds = require('./birds');
app.use('/birds', birds);Koa Asynchronous Middleware (Onion Model)
Koa improves on Express by using async functions that return Promise , allowing clean pre‑ and post‑processing via the onion model. This enables advanced scenarios such as unified error handling and request timing with minimal code.
MVC Pattern
The article argues that while MVC is ubiquitous, its server‑side form remains valuable for separating concerns: models handle data, views render output, and controllers orchestrate flow. This separation improves reuse, testability, and reduces complexity, especially as applications grow.
Dependency Injection (DI) Pattern
DI is presented as an implementation of Inversion of Control, allowing services like session stores to be swapped without changing business logic. In dynamic languages like JavaScript, DI can be achieved through conventions and framework helpers such as extend and adapter in ThinkJS.
Visual diagrams (omitted) illustrate how IoC containers instantiate and inject dependencies at runtime.
Conclusion
The three patterns—Middleware, MVC, and DI—appear across most web frameworks, demonstrating that well‑defined architectural patterns provide a common vocabulary, improve abstraction, and facilitate communication and scalability in backend development.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.