JavaScript Notes
Complete guide to JavaScript backend frameworks including Node.js runtime, Express.js, Fastify, NestJS, and Hono with real code examples, outputs, architecture diagrams, comparison tables, and interview questions for beginners and intermediate developers.
Introduction
JavaScript backend frameworks allow developers to build server-side applications, REST APIs, real-time services, and microservices using the same language they use on the frontend. With Node.js as the runtime foundation, an entire ecosystem of frameworks has emerged — each solving different problems and targeting different use cases.
Why Backend Frameworks Matter:
- They handle HTTP requests, routing, middleware, and response formatting
- They provide structure for building scalable server applications
- They abstract away low-level networking details
- They enable rapid API and microservice development
In this guide, we'll cover the most important JavaScript backend frameworks in 2026: Node.js (the runtime), Express.js (the classic), Fastify (performance-first), NestJS (enterprise-grade), and Hono (edge/serverless). You'll see real code examples with outputs for each one.
Node.js — The Runtime Foundation
Node.js is not a framework — it's a JavaScript runtime built on Chrome's V8 engine. It allows JavaScript to run outside the browser, enabling server-side programming.
Key characteristics:
- Non-blocking I/O — handles many connections simultaneously without threads
- Event-driven architecture — uses an event loop for async operations
- Single-threaded — one main thread with worker threads for heavy computation
- npm ecosystem — millions of packages available
Basic HTTP Server with Node.js (No Framework)
Server running on http://localhost:3000
// GET http://localhost:3000/
Hello from raw Node.js server!
// GET http://localhost:3000/json
{"message":"This is JSON","status":"ok"}
// GET http://localhost:3000/unknown
Route not foundWhy frameworks exist: Writing raw Node.js servers means manually handling routing, parsing, error handling, and middleware. Frameworks automate these patterns.
Express.js — The Most Popular Framework
Express.js is the most widely used Node.js framework. It's minimalist, unopinionated, and flexible — providing just enough structure for building web applications and APIs.
Why Express dominates:
- Minimal learning curve
- Massive community and middleware ecosystem
- Flexible — doesn't force architectural patterns
- Battle-tested in production (Netflix, PayPal, Uber)
Example 1: Express Hello World Server
Express server running on port 3000
// GET http://localhost:3000/
Hello World from Express!
// GET http://localhost:3000/about
{"framework":"Express.js","version":"4.x","type":"minimalist"}Example 2: Route Handling with Parameters
Server running on port 3000
// GET http://localhost:3000/users/42
{"message":"Fetching user with ID: 42"}
// GET http://localhost:3000/search?q=javascript&page=2
{"query":"javascript","page":2,"results":[]}
// GET http://localhost:3000/posts
{"action":"GET all posts"}
// POST http://localhost:3000/posts
{"action":"CREATE new post"}Example 3: Middleware in Express
Middleware functions execute in order during the request-response cycle. They have access to req, res, and next.
Server running on port 3000
// GET http://localhost:3000/public
[2026-06-12T10:00:00.000Z] GET /public
{"message":"This is public"}
// GET http://localhost:3000/dashboard (no auth header)
[2026-06-12T10:00:01.000Z] GET /dashboard
{"error":"Unauthorized"}
// GET http://localhost:3000/dashboard (with Authorization: Bearer secret123)
[2026-06-12T10:00:02.000Z] GET /dashboard
{"message":"Welcome Admin","data":"sensitive info"}Example 4: REST API Pattern with Express
REST API running on port 3000
// GET http://localhost:3000/api/todos
{"count":2,"data":[{"id":1,"title":"Learn Express","completed":true},{"id":2,"title":"Build REST API","completed":false}]}
// GET http://localhost:3000/api/todos/1
{"id":1,"title":"Learn Express","completed":true}
// POST http://localhost:3000/api/todos Body: {"title":"Learn Fastify"}
{"id":3,"title":"Learn Fastify","completed":false}
// PUT http://localhost:3000/api/todos/2 Body: {"completed":true}
{"id":2,"title":"Build REST API","completed":true}
// DELETE http://localhost:3000/api/todos/1
{"message":"Todo deleted successfully"}Fastify — Performance-First Framework
Fastify is designed to be the fastest Node.js web framework. It uses JSON Schema for validation and serialization, achieving significant performance gains over Express.
Key features:
- ~2-3x faster than Express in benchmarks
- Built-in JSON Schema validation
- Plugin architecture (encapsulation)
- TypeScript-first support
- Automatic Swagger/OpenAPI documentation
Example 5: Fastify Server with Validation
Fastify server running on port 3000
// GET http://localhost:3000/
{"hello":"world","framework":"Fastify"}
// POST http://localhost:3000/users Body: {"name":"Rahul","email":"rahul@example.com"}
{"id":1718193600000,"name":"Rahul","email":"rahul@example.com","createdAt":"2026-06-12T10:00:00.000Z"}
// POST http://localhost:3000/users Body: {"name":"R"} (validation fails)
{"statusCode":400,"error":"Bad Request","message":"body/name must NOT have fewer than 2 characters"}
// GET http://localhost:3000/users/7
{"userId":"7","name":"User_7","active":true}Why Fastify is faster: It pre-compiles JSON serialization using fast-json-stringify, avoids regex-based routing (uses find-my-way radix tree), and uses schema-based optimizations.
NestJS — Enterprise-Grade Framework
NestJS is a progressive TypeScript framework inspired by Angular. It provides a complete application architecture with dependency injection, modules, decorators, and built-in support for microservices, GraphQL, and WebSockets.
Key features:
- Full TypeScript support with decorators
- Dependency injection (IoC container)
- Modular architecture
- Built-in support for testing
- Can use Express or Fastify under the hood
Example 6: NestJS Controller Pattern
// GET http://localhost:3000/users
{"count":2,"data":[{"id":1,"name":"Amit","email":"amit@example.com","role":"admin"},{"id":2,"name":"Priya","email":"priya@example.com","role":"user"}]}
// GET http://localhost:3000/users/1
{"id":1,"name":"Amit","email":"amit@example.com","role":"admin"}
// POST http://localhost:3000/users Body: {"name":"Vikram","email":"vikram@example.com","role":"developer"}
{"message":"User created","user":{"id":3,"name":"Vikram","email":"vikram@example.com","role":"developer"}}NestJS Architecture: Module → Controller → Service → Repository. Each layer has a single responsibility, making large applications maintainable.
Hono — Edge & Serverless Framework
Hono is an ultrafast, lightweight framework designed for edge computing and serverless environments (Cloudflare Workers, Deno Deploy, AWS Lambda, Bun). It has zero dependencies and works across all JavaScript runtimes.
Key features:
- Ultra-small bundle size (~14KB)
- Works on every runtime (Node.js, Deno, Bun, Cloudflare Workers)
- RegExpRouter for blazing fast routing
- Built-in middleware (cors, jwt, logger, etc.)
- TypeScript-first with RPC-like client
Hono Basic Example
// GET /
{"message":"Hello from Hono!","runtime":"edge"}
// GET /users/Ravi
{"greeting":"Hello, Ravi!","timestamp":1718193600000}
// POST /data Body: {"key":"value"}
{"received":{"key":"value"},"processed":true}Comparison Table
| Feature | Express.js | Fastify | NestJS | Hono |
|---|---|---|---|---|
| Performance | Good | Excellent (2-3x Express) | Good (uses Express/Fastify) | Excellent (edge-optimized) |
| Learning Curve | Low | Low-Medium | High | Low |
| TypeScript | Manual setup | Built-in support | Native TypeScript | Native TypeScript |
| Architecture | Unopinionated | Plugin-based | Opinionated (Angular-like) | Minimal |
| Validation | Manual (add joi/zod) | Built-in JSON Schema | Built-in (class-validator) | Built-in (zod integration) |
| Bundle Size | ~200KB | ~300KB | ~2MB+ | ~14KB |
| Best For | Quick APIs, prototypes | High-throughput APIs | Enterprise apps | Edge/serverless |
| Middleware | Large ecosystem | Plugin system | Decorators + Guards | Built-in helpers |
| Community | Largest | Growing fast | Large | Growing fast |
| Production Users | Netflix, PayPal, Uber | Walmart, Condé Nast | Adidas, Roche | Cloudflare, Vercel |
| Release Year | 2010 | 2016 | 2017 | 2021 |
Serving JSON — Common Pattern Across Frameworks
// GET /api/data (same for all frameworks)
Content-Type: application/json
{"items":[1,2,3],"total":3}When to Use Which Framework
Choose Express.js when:
- You're building a quick prototype or MVP
- Your team is new to Node.js backend development
- You need maximum flexibility in architecture choices
- You want the largest middleware ecosystem
- Project size is small to medium
Choose Fastify when:
- Performance and throughput are critical
- You're building high-traffic APIs
- You want built-in validation without extra packages
- You prefer a plugin-based architecture
- You need automatic API documentation
Choose NestJS when:
- You're building a large enterprise application
- Your team comes from Angular or Spring Boot background
- You need dependency injection and modular architecture
- You want built-in support for microservices, GraphQL, WebSockets
- Long-term maintainability is a priority
Choose Hono when:
- You're deploying to edge/serverless (Cloudflare Workers, Deno Deploy)
- Bundle size matters (serverless cold starts)
- You want runtime portability (Node, Deno, Bun)
- You're building lightweight APIs or middleware layers
- You want modern developer experience with minimal overhead
Error Handling Pattern
Server running on port 3000
// GET http://localhost:3000/risky
Error: Unexpected token i in JSON at position 0
{"error":"Internal Server Error","message":"Unexpected token i in JSON at position 0","timestamp":"2026-06-12T10:00:00.000Z"}Key Takeaways
- Node.js is a runtime, not a framework — it provides the foundation (V8 engine, event loop, non-blocking I/O) on which all JavaScript backend frameworks are built.
- Express.js is the industry standard — with 60,000+ GitHub stars and 15+ years of maturity, it remains the most used Node.js framework for good reason: simplicity and flexibility.
- Middleware is the backbone of Express — every request passes through a chain of middleware functions (logging, parsing, auth, etc.) before reaching the route handler.
- Fastify offers 2-3x better performance than Express through JSON Schema pre-compilation, radix-tree routing, and optimized serialization.
- NestJS is ideal for enterprise applications — its modular architecture with dependency injection, decorators, and TypeScript-first approach makes large codebases maintainable.
- Hono targets the edge — at ~14KB with zero dependencies, it's perfect for serverless environments where cold start time and bundle size matter.
- All frameworks follow the same core pattern — receive request → process through middleware → match route → execute handler → send response.
- JSON Schema validation (Fastify) vs class-validator (NestJS) vs manual (Express) — each framework handles input validation differently, but all require it for production apps.
- Framework choice depends on context — there's no universally "best" framework. Team size, performance needs, deployment target, and project complexity all factor in.
- The ecosystem evolves rapidly — frameworks like Hono and Elysia (Bun-native) represent the next generation optimized for modern runtimes and edge computing.
Interview Questions
Q1: What is the difference between Node.js and Express.js?
Answer: Node.js is a JavaScript runtime environment built on Chrome's V8 engine that allows JavaScript to run on the server. It provides core modules like http, fs, and path. Express.js is a web application framework built on top of Node.js that provides higher-level abstractions like routing, middleware chains, and request/response utilities. You can build a server with raw Node.js, but Express makes it significantly easier with cleaner syntax and patterns.
Q2: Explain the Express.js middleware execution order.
Answer: Middleware in Express executes sequentially in the order it is defined. Each middleware has access to req, res, and next(). When next() is called, control passes to the next middleware. If next() is not called, the request-response cycle ends there. Application-level middleware (app.use()) runs before route-specific middleware. Error-handling middleware (4 parameters) catches errors passed via next(err).
Q3: Why is Fastify faster than Express?
Answer: Fastify achieves better performance through: (1) JSON Schema-based serialization using fast-json-stringify which pre-compiles serializers, (2) Radix tree routing via find-my-way instead of regex matching, (3) Optimized request lifecycle with fewer allocations, and (4) Plugin encapsulation that avoids middleware overhead. Benchmarks show Fastify handling 30,000-75,000 requests/second compared to Express's 15,000-25,000.
Q4: What is dependency injection in NestJS and why does it matter?
Answer: Dependency injection (DI) is a design pattern where a class receives its dependencies from an external source rather than creating them internally. In NestJS, the IoC (Inversion of Control) container manages object creation and lifecycle. This matters because: (1) it makes code testable (you can inject mocks), (2) promotes loose coupling between modules, (3) enables singleton management of shared services, and (4) makes the architecture scalable for large teams.
Q5: When would you choose Hono over Express?
Answer: Choose Hono when: (1) Deploying to edge/serverless platforms like Cloudflare Workers or Deno Deploy where bundle size affects cold starts, (2) Runtime portability is needed (same code runs on Node, Deno, Bun), (3) you want TypeScript-first development with strong type inference, (4) you need minimal overhead for simple API gateways or middleware layers. Choose Express when you need the largest middleware ecosystem, team familiarity, or extensive community resources.
Frequently Asked Questions (FAQ)
FAQ 1: Can I use Express.js with TypeScript?
Yes! While Express wasn't built with TypeScript, you can add TypeScript support by installing @types/express and configuring tsconfig.json. However, the experience isn't as seamless as Fastify or NestJS where TypeScript is a first-class citizen. For TypeScript-heavy projects, consider NestJS or Fastify.
FAQ 2: Is Express.js dying in 2026?
No. Express.js continues to receive updates (Express 5.x is stable) and remains the most downloaded Node.js framework with 30+ million weekly npm downloads. While newer frameworks offer better performance or features, Express's simplicity, ecosystem, and community ensure it stays relevant. Many companies maintain Express applications that work perfectly fine.
FAQ 3: Can NestJS use Fastify instead of Express internally?
Yes! NestJS is platform-agnostic — it can use either Express or Fastify as its underlying HTTP platform. To switch to Fastify, install @nestjs/platform-fastify and change the adapter in main.ts. This gives you NestJS's architecture with Fastify's performance benefits.
FAQ 4: What's the difference between REST APIs and GraphQL in backend frameworks?
REST uses multiple endpoints with fixed data structures (GET /users, GET /users/1), while GraphQL uses a single endpoint where the client specifies exactly what data it needs. Express and Fastify are primarily for REST APIs. NestJS has built-in GraphQL support via @nestjs/graphql. Hono supports both through middleware. Choose REST for simple CRUD, GraphQL for complex data requirements with multiple frontends.
FAQ 5: How do I deploy a Node.js backend framework application?
Common deployment options: (1) Traditional servers — AWS EC2, DigitalOcean Droplets with PM2 process manager, (2) Containers — Docker + Kubernetes for scalability, (3) Platform-as-a-Service — Railway, Render, Heroku, (4) Serverless — AWS Lambda (with express adapter), Vercel Functions, (5) Edge — Cloudflare Workers (Hono), Deno Deploy. For production, always use environment variables, health checks, graceful shutdown handlers, and a reverse proxy (Nginx/Caddy) in front of your Node.js application.
Summary
JavaScript backend frameworks transform Node.js from a raw runtime into a productive platform for building web servers, APIs, and microservices. Express.js remains the go-to choice for most projects due to its simplicity and ecosystem. Fastify wins when performance is critical. NestJS shines in large enterprise applications needing strict architecture. Hono represents the future of edge-native JavaScript backends. Understanding the trade-offs between these frameworks — and when to use each — is a core skill for any JavaScript developer building server-side applications in 2026.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Backend Frameworks — Node.js, Express, NestJS Complete Guide 2026.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, ecosystem, backend, frameworks
Related JavaScript Master Course Topics