NestJS HTTP Adapters: Express vs Fastify

NestJS HTTP Adapters: Express vs Fastify

Understand how NestJS abstracts underlying HTTP servers through its adapter architecture. Compare Express, Fastify, and the native Node.js adapter (@nestjs/platform-node) across throughput, serialization, and compatibility.

NestJSNode.jsFastifyExpress.jsBackend

Why NestJS Abstracts the HTTP Layer

Node.js Ecosystem မှာ Framework အများစုဟာ သတ်မှတ်ထားတဲ့ HTTP Server တစ်ခုတည်းအပေါ်မှာသာ တိုက်ရိုက် တည်ဆောက်ထားလေ့ ရှိပါတယ်။ ဒါပေမဲ့ NestJS ရဲ့ ထူးခြားတဲ့ Architectural Design ကတော့ Controllers, Guards, Interceptors, Pipes နဲ့ Exception Filters တွေအားလုံးကို အောက်ခြေ HTTP Engine နဲ့ တိုက်ရိုက် မချိတ်ဆက်ဘဲ AbstractHttpAdapter ဆိုတဲ့ Abstraction Layer တစ်ခုခံပြီး တည်ဆောက်ထားခြင်း ဖြစ်ပါတယ်။

ဒီ Adapter Pattern ကြောင့် Developers တွေဟာ Application Business Logic ကို ပြောင်းလဲစရာမလိုဘဲ အောက်ခြေ HTTP Engine အဖြစ် Ecosystem ကြီးမားတဲ့ Express (@nestjs/platform-express)၊ စွမ်းဆောင်ရည် အလွန်မြင့်မားတဲ့ Fastify (@nestjs/platform-fastify) သို့မဟုတ် Third-Party Framework လုံးဝမခံဘဲ Node.js Core ပေါ်မှာ တိုက်ရိုက် အလုပ်လုပ်တဲ့ Native Node Adapter (@nestjs/platform-node) တို့ကို Project လိုအပ်ချက်အလိုက် လွတ်လပ်စွာ ရွေးချယ်နိုင်ပါတယ်။

Loading flowchart...

Express Adapter: Ecosystem Maturity and Default Ergonomics

NestJS Project အသစ်တစ်ခု ဖန်တီးလိုက်တဲ့အခါ ကာလရှည်ကြာ Default အနေနဲ့ ပါဝင်လာတာကတော့ @nestjs/platform-express ဖြစ်ပါတယ်။ Express ကို အသုံးများရတဲ့ အဓိက အကြောင်းရင်းကတော့ Node.js Community တစ်ခုလုံးရဲ့ Libraries သောင်းပေါင်းများစွာဟာ Express ရဲ့ (req, res, next) Middleware Signature အပေါ်မှာ အခြေခံပြီး ရေးသားထားကြလို့ ဖြစ်ပါတယ်။

  • Zero-Friction Integration: Passport.js Authentication Strategies တွေ၊ multer File Uploaders တွေ၊ helmet၊ compression နဲ့ Third-Party Webhook SDKs အများစုဟာ Express Adapter ပေါ်မှာ Configuration အပိုလုပ်စရာမလိုဘဲ တိုက်ရိုက် အလုပ်လုပ်နိုင်ပါတယ်။
  • Developer Familiarity: Node.js Developer တိုင်းနီးပါးဟာ Express ရဲ့ Request နဲ့ Response Lifecycle ကို ရင်းနှီးပြီးသား ဖြစ်တဲ့အတွက် Onboarding နဲ့ Debugging လုပ်ရတာ အလွန်လွယ်ကူပါတယ်။

ဒါပေမဲ့ Express ရဲ့ Routing Engine ဟာ Linear Regular Expression Matching ပေါ်မှာ အခြေခံထားပြီး၊ Request ဝင်လာတိုင်း Prototype Monkey-Patching နဲ့ Dynamic JSON.stringify() ကို တွက်ချက်ရတဲ့အတွက် တစ်စက္ကန့်ကို Requests သောင်းချီဝင်တဲ့ High-Concurrency Workloads တွေမှာ CPU Bottleneck ဖြစ်ပေါ်လာတတ်ပါတယ်။

Fastify Adapter: Radix-Tree Routing and High Throughput

Microservices တွေနဲ့ Latency အလွန်နည်းပါးဖို့ လိုအပ်တဲ့ High-Throughput APIs တွေအတွက် @nestjs/platform-fastify က Express ထက် 2x ကနေ 3x အထိ ပိုမိုမြန်ဆန်တဲ့ Requests Per Second (RPS) ကို ပေးစွမ်းနိုင်ပါတယ်။ Fastify မြန်ဆန်ရခြင်းရဲ့ နောက်ကွယ်မှာ အင်ဂျင်နီယာ နည်းပညာ ၃ ခု ရှိပါတယ်။

  1. Radix-Tree Router (find-my-way): Express လို Routes တစ်ခုချင်းစီကို RegExp နဲ့ လိုက်တိုက်စစ်မယ့်အစား URL Paths တွေကို Prefix Radix Tree တည်ဆောက်ပြီး ရှာဖွေတဲ့အတွက် Routes ရာပေါင်းများစွာ ရှိနေရင်တောင် Routing Lookup Time ဟာ အလွန်မြန်ဆန်ပါတယ်။
  2. Schema-Based JSON Serialization (fast-json-stringify): Response Schema သတ်မှတ်ပေးထားတဲ့အခါ Fastify က V8 Engine အတွက် အထူးပြုလုပ်ထားတဲ့ Type-Specific String Concatenation Function ကို ကြိုတင် Compile လုပ်ထားတဲ့အတွက် သာမန် JSON.stringify() ထက် နှစ်ဆခန့် ပိုမြန်ပါတယ်။
  3. Zero-Allocation Pino Logger & Object Pooling: Request တစ်ခုချင်းစီအတွက် Heavy Wrapper Objects တွေ အသစ်မဆောက်ဘဲ Memory Allocation နည်းပါးအောင် ဒီဇိုင်းဆွဲထားပါတယ်။

သတိထားရမယ့် အချက်တစ်ခုကတော့ Express က Default အနေနဲ့ Network Interfaces အားလုံး (0.0.0.0) မှာ နားထောင်ပေးပေမဲ့၊ Fastify ကတော့ 127.0.0.1 (localhost) မှာသာ Default နားထောင်ပါတယ်။ ဒါကြောင့် Docker Container သို့မဟုတ် Kubernetes Pod ထဲမှာ Fastify ကို Run တဲ့အခါ await app.listen(3000, '0.0.0.0') လို့ အတိအကျ သတ်မှတ်ပေးရပါမယ်။

Native Node Adapter: Zero-Framework Overhead with @nestjs/platform-node

Express ရဲ့ Middleware Compatibility ကိုလည်း လိုချင်သလို Fastify နီးပါး မြန်ဆန်တဲ့ Performance ကိုပါ ရရှိစေဖို့ NestJS က အသစ်မိတ်ဆက်လိုက်တဲ့ တတိယမြောက် ရွေးချယ်မှုကတော့ @nestjs/platform-node (NodeAdapter) ပဲ ဖြစ်ပါတယ်။ NestJS v12.3+ မှာ Opt-in အနေနဲ့ စတင်အသုံးပြုနိုင်ပြီး အနာဂတ် NestJS v13 မှာ Default HTTP Adapter ဖြစ်လာဖို့ ရည်ရွယ်ထားတဲ့ Modern Adapter ဖြစ်ပါတယ်။

NodeAdapter ရဲ့ ထူးခြားချက်ကတော့ Express သို့မဟုတ် Fastify လို ပြင်ပ Web Framework တစ်ခုလုံးကို ကြားခံအနေနဲ့ မသုံးတော့ဘဲ၊ Node.js ရဲ့ Built-in node:http (IncomingMessage နဲ့ ServerResponse) အပေါ်မှာ တိုက်ရိုက် တည်ဆောက်ထားခြင်း ဖြစ်ပါတယ်။

  • Direct Node.js Pipeline: ကြားခံ Framework Abstraction မရှိတော့တဲ့အတွက် ExpressAdapter ထက် Requests Per Second 50%+ ပိုမိုမြန်ဆန်ပြီး Memory Footprint လည်း သိသာစွာ သက်သာပါတယ်။
  • Express-Style Helper Layer: အောက်ခြေမှာ node:http ကို သုံးထားပေမဲ့ Request နဲ့ Response Objects တွေပေါ်မှာ Express-style Helpers (res.status(), res.json(), req.query, req.cookies) တွေကို ပေါ့ပါးစွာ ထည့်သွင်းပေးထားတဲ့အတွက် ရှိပြီးသား Express Middlewares အများစုကို Code ပြင်စရာမလိုဘဲ ဆက်လက်အသုံးပြုနိုင်ပါတယ်။
import { NestFactory } from '@nestjs/core'
import { NodeAdapter, NestNodeApplication } from '@nestjs/platform-node'
import { AppModule } from './app.module'

async function bootstrap() {
  const app = await NestFactory.create<NestNodeApplication>(
    AppModule,
    new NodeAdapter()
  )

  await app.listen(process.env.PORT ?? 3000, '0.0.0.0')
}
bootstrap()

Comparing Express, Fastify, and Native Node Adapters

Project တစ်ခုအတွက် ဘယ် Adapter ကို ရွေးချယ်သင့်လဲဆိုတာ အောက်ပါ အင်ဂျင်နီယာ နှိုင်းယှဉ်ချက်ဇယားမှာ ကြည့်ရှုနိုင်ပါတယ်။

Engineering DimensionExpress (platform-express)Fastify (platform-fastify)Native Node (platform-node)
Underlying EngineExpress.js frameworkFastify frameworkNative node:http / node:https
Throughput (RPS)~15k–25k req/sec (baseline)~45k–70k+ req/sec (2x–3x)~35k–55k+ req/sec (~1.5x–2x+)
Middleware Support100% Express (req, res, next)Fastify hooks / @fastify/middieExpress-style helpers built-in
Dependency WeightHeavy legacy dependency treeModerate (@fastify/* plugins)Minimal (zero external framework)
Best Engineering FitLegacy Express-heavy appsUltra-low-latency microservicesModern default for greenfield APIs

Platform-Agnostic vs Platform-Native APIs (@Req and @Res)

NestJS သုံးတဲ့အခါ အဖြစ်အများဆုံး အမှားတစ်ခုကတော့ Controller Methods တွေထဲမှာ @Res() res: Response ကို ထည့်သွင်းပြီး res.status(200).send() လို့ တိုက်ရိုက် ခေါ်သုံးမိခြင်း ဖြစ်ပါတယ်။

@Res() ကို သုံးလိုက်တဲ့အခါ ဆိုးကျိုး ၂ ခု ဖြစ်ပေါ်ပါတယ်။

  1. Breaking NestJS Interceptors: NestJS ရဲ့ Standard Response Lifecycle ပိတ်သွားတဲ့အတွက် ClassSerializerInterceptor၊ Global Response Transform Interceptors နဲ့ @HttpCode() Decorators တွေ အလုပ်မလုပ်တော့ပါဘူး။ (Cookies သို့မဟုတ် Headers သတ်မှတ်ချင်ရုံဆိုရင် @Res({ passthrough: true }) ကိုသာ သုံးရပါမယ်။)
  2. Adapter Lock-in: Express ရဲ့ res.json() သို့မဟုတ် req.ip ကို Controller ထဲမှာ တိုက်ရိုက် ခေါ်သုံးထားမိရင် နောက်ပိုင်းမှာ FastifyAdapter သို့ ပြောင်းလဲတဲ့အခါ Runtime Errors တွေ တက်လာမှာ ဖြစ်ပါတယ်။
import { Controller, Post, Res, HttpStatus } from '@nestjs/common'
import type { FastifyReply } from 'fastify'

@Controller('auth')
export class AuthController {
  @Post('session')
  createSession(@Res({ passthrough: true }) reply: FastifyReply) {
    reply.header('X-Platform-Engine', 'Fastify')
    reply.status(HttpStatus.CREATED)
    return { authenticated: true }
  }
}

အထက်ပါ ဥပမာအတိုင်း @Res({ passthrough: true }) ကို သုံးထားတဲ့အတွက် Platform-Native Header နဲ့ Status Code ကိုလည်း သတ်မှတ်နိုင်သလို၊ Return ပြန်လိုက်တဲ့ Object ကလည်း NestJS Interceptors နဲ့ Fastify Serializer တွေကို ပုံမှန်အတိုင်း ဖြတ်သန်းသွားနိုင်မှာ ဖြစ်ပါတယ်။

Adapter-Agnostic Access with HttpAdapterHost

အကယ်၍ Custom Exception Filter သို့မဟုတ် Global Logging Interceptor တစ်ခု ရေးသားတဲ့အခါ၊ အောက်ခံ Engine က Express လား Fastify လား ဆိုတာကို ကြိုတင်မသိနိုင်ဘဲ လက်ရှိ Active ဖြစ်နေတဲ့ Adapter နဲ့ Response ပို့ချင်တဲ့ အခြေအနေမျိုး ရှိတတ်ပါတယ်။ အဲဒီလို အခါမျိုးမှာ NestJS ရဲ့ HttpAdapterHost ကို Inject လုပ်ပြီး Platform-Agnostic Methods (httpAdapter.reply(), httpAdapter.getRequestUrl()) တွေကို ခေါ်သုံးနိုင်ပါတယ်။

import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common'
import { HttpAdapterHost } from '@nestjs/core'

@Catch(HttpException)
export class AllExceptionsFilter implements ExceptionFilter {
  constructor(private readonly httpAdapterHost: HttpAdapterHost) {}

  catch(exception: HttpException, host: ArgumentsHost) {
    const { httpAdapter } = this.httpAdapterHost
    const ctx = host.switchToHttp()
    const status = exception.getStatus()

    const body = {
      statusCode: status,
      path: httpAdapter.getRequestUrl(ctx.getRequest()),
      timestamp: new Date().toISOString(),
    }

    httpAdapter.reply(ctx.getResponse(), body, status)
  }
}

ဒီနည်းလမ်းကို သုံးထားတဲ့အခါ ကိုယ့်ရဲ့ Shared Libraries တွေ၊ Exception Filters တွေနဲ့ Audit Interceptors တွေဟာ platform-express၊ platform-fastify နဲ့ platform-node တို့အားလုံးမှာ Code တစ်ကြောင်းမှ ပြင်စရာမလိုဘဲ တသမတ်တည်း အလုပ်လုပ်နိုင်ပါတယ်။

ဒါ့အပြင် Express ကနေ Fastify သို့ ပြောင်းလဲရာမှာ သတိထားရမယ့် လက်တွေ့ Migration အချက်အလက်တွေလည်း ရှိပါတယ်။ Express မှာ လူသုံးများတဲ့ helmet, compression နဲ့ cookie-parser တို့အစား Fastify မှာတော့ သူ့ရဲ့ Encapsulated Plugin Architecture နဲ့ ကိုက်ညီတဲ့ @fastify/helmet, @fastify/compress နဲ့ @fastify/cookie တို့ကို app.register() ကနေတစ်ဆင့် ချိတ်ဆက်ပေးရပါတယ်။ အလားတူပဲ File Upload အတွက် Express ရဲ့ Memory-buffered Multer အစား @fastify/multipart ရဲ့ Asynchronous Stream API (req.parts()) ကို သုံးခြင်းအားဖြင့် Large Files တွေ Upload လုပ်ချိန်မှာ Node.js Heap Memory ပြည့်သွားခြင်းမှ အပြည့်အဝ ကာကွယ်နိုင်ပါတယ်။

Final Thoughts

Express၊ Fastify နဲ့ Native Node (@nestjs/platform-node) Adapters ၃ ခုလုံးဟာ NestJS ရဲ့ အားသာချက်ကြီးတွေ ဖြစ်ကြပါတယ်။ Legacy Express Libraries အများအပြားကို နက်နက်ရှိုင်းရှိုင်း မှီခိုထားရတဲ့ Applications တွေအတွက် Express Adapter က သင့်တော်ပြီး၊ Third-Party Framework Overhead မပါဘဲ Express-style Ergonomics နဲ့ မြင့်မားတဲ့ Performance ကို တစ်ပြိုင်တည်း လိုချင်တဲ့ Greenfield Projects တွေအတွက် @nestjs/platform-node (NodeAdapter) က အကောင်းဆုံး ရွေးချယ်မှု ဖြစ်ပါတယ်။ အမြင့်ဆုံး Throughput၊ Schema-compiled JSON Serialization နဲ့ Dedicated Plugin Ecosystem လိုအပ်တဲ့ Ultra-Low-Latency Microservices တွေအတွက်ကတော့ Fastify Adapter၊ @Res({ passthrough: true }) နဲ့ HttpAdapterHost ပုံစံတို့ကို ပေါင်းစပ်အသုံးချနိုင်ပါတယ်။