NestJS Express Middleware and Fastify Runtime Interoperability in Version 10
0 reputation · 28 Jan 2021, 22:37 UTC
0 reputation · 28 Jan 2021, 22:37 UTC
NestJS 10 changed the default HTTP server from Express to Fastify, creating an integration boundary for teams that maintain Express-style middleware across version 9 and version 10 codebases. The precise goal is to determine which existing Express middleware patterns remain viable under the Fastify runtime without introducing runtime failures or hidden behavioral differences.
The relevant uncertainty centers on the absence of a definitive compatibility list in the official migration documentation. While the framework supports both server adapters, many third-party middlewares assume Express 4.x request and response objects, and it is unclear which of those assumptions are guaranteed to fail or silently degrade. The optional Express shim offers a bridge, but its long-term maintenance status and performance characteristics are not clearly documented.
Given this ambiguity, what validation strategy should teams apply before committing to either rewriting middleware for Fastify or relying on the Express compatibility layer? Which categories of Express middleware are most likely to break under the Fastify adapter, and how can those risks be identified without exhaustive trial and error? Is there an official compatibility matrix that distinguishes supported Express APIs from those that require replacement in NestJS 10?
35775 reputation · 29 Jan 2021, 07:50 UTC
There is no official compatibility matrix in NestJS 10 documentation that distinguishes supported Express APIs from those requiring replacement. The framework maintains platform-agnostic abstractions for controllers and pipes, but middleware registered via app.use() or Module.middleware() executes directly on the underlying HTTP engine—Express in v9, Fastify by default in v10. Any middleware that calls methods on the res object (res.send(), res.json(), res.cookie(), res.setHeader()) will fail at runtime under Fastify because Fastify's Reply object exposes a different API. Middleware that only reads or mutates req (adding properties, parsing bodies, extracting headers) has a higher chance of working, provided it does not invoke Express-specific methods like req.accepts() or req.ip.
res., response., or reply. methods. Flag any usage of send, json, status, cookie, redirect, setHeader, getHeader, removeHeader.res.send()), cookie/session managers, CORS implementations that mutate res.res.setHeader().req.user, req.requestId), pure validation middleware that only throws or calls next().@nestjs/platform-fastify, register only the flagged middleware, and hit the endpoint with a test client. Verify the Fastify Reply object is not accessed incorrectly. This takes minutes per middleware and avoids full-app trial-and-error.express-session, csurf) has no Fastify-native equivalent, plan a rewrite. Otherwise, migrate incrementally using Fastify hooks (onRequest, preHandler) for new code.The @nestjs/platform-express adapter remains available in v10 and can be swapped in with NestFactory.create(AppModule, new ExpressAdapter()). This is a supported, non-deprecated path—not a "shim" with unclear maintenance. However, it forfeits Fastify's performance benefits (throughput, serialization speed) and adds Express as a runtime dependency. Treat this as a deliberate architectural choice, not a temporary bridge. If you choose Fastify, rewrite middleware using Fastify's hook system or use packages with explicit Fastify support (e.g., @fastify/cookie, @fastify/cors, @fastify/helmet).
What percentage of your current middleware stack is third-party vs. custom? This changes the rewrite effort estimate significantly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.