Building Scalable Backend Services with Node.js, Express.js, and NestJS

Manish AgrawalManish Agrawal
3 min read

Introduction

Node.js has revolutionized backend development by enabling JavaScript to run on the server side. With frameworks like Express.js (minimalist and flexible) and NestJS (structured and TypeScript-based), developers can build efficient, scalable APIs and microservices.

In this post, we’ll explore:

  • Node.js fundamentals for backend development

  • Express.js for lightweight REST APIs

  • NestJS for enterprise-grade applications

  • Performance optimization and best practices


1. Node.js: The Backbone of JavaScript Backend

Node.js is a runtime built on Chrome’s V8 engine, allowing JavaScript to execute outside the browser. Key features:

  • Non-blocking I/O (Event Loop)

  • Single-threaded but scalable (via Worker Threads & Clustering)

  • NPM ecosystem (largest open-source library registry)

Example: Simple HTTP Server

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js!');
});

server.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

2. Express.js: Fast & Minimalist Web Framework

Express.js simplifies routing, middleware, and HTTP handling.

Key Features

  • Middleware support (e.g., body-parser, cors)

  • REST API routing (app.get, app.post)

  • Template engines (EJS, Pug)

  • Error handling middleware

Example: REST API with Express

const express = require('express');
const app = express();

// Middleware
app.use(express.json());

// Routes
app.get('/api/users', (req, res) => {
  res.json([{ id: 1, name: 'John' }]);
});

app.post('/api/users', (req, res) => {
  const user = req.body;
  res.status(201).json(user);
});

// Error Handling
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});

app.listen(3000, () => {
  console.log('Express server running on http://localhost:3000');
});

Performance Tips

  • Use helmet for security headers

  • Compress responses with compression

  • Rate limiting with express-rate-limit

  • Use a reverse proxy (Nginx) for load balancing


3. NestJS: Enterprise-Grade Framework

NestJS is built on Express.js (or Fastify) but introduces:

  • Modular architecture (Modules, Controllers, Providers)

  • Dependency Injection (DI)

  • TypeScript support

  • Microservices-ready

Example: NestJS CRUD API

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

// app.module.ts
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
  controllers: [UsersController],
  providers: [UsersService],
})
export class AppModule {}

// users.controller.ts
import { Controller, Get, Post, Body } from '@nestjs/common';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  getUsers() {
    return this.usersService.findAll();
  }

  @Post()
  createUser(@Body() user) {
    return this.usersService.create(user);
  }
}

// users.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {
  private users = [{ id: 1, name: 'John' }];

  findAll() {
    return this.users;
  }

  create(user) {
    this.users.push(user);
    return user;
  }
}

Why NestJS?

  • Structured architecture (similar to Angular)

  • Built-in support for WebSockets, GraphQL, gRPC

  • Easy testing (Jest integration)

  • Works with Fastify for better performance


4. Performance Optimization

Node.js & Express.js

  • Use cluster module for multi-core CPU utilization

  • Enable gzip compression

  • Cache responses (Redis)

  • Avoid blocking the event loop (offload CPU-heavy tasks to Workers)

NestJS

  • Use FastifyAdapter (faster than Express)

  • Lazy-load modules

  • Implement CQRS for complex apps


5. Express.js vs. NestJS

FeatureExpress.jsNestJS
Learning CurveLowModerate
StructureFlexibleOpinionated
TypeScriptOptionalFirst-class
MicroservicesManual setupBuilt-in
Enterprise UseGoodBest

Choose Express.js if:

  • You need a lightweight API

  • You prefer flexibility over structure

Choose NestJS if:

  • You’re building a large-scale app

  • You want TypeScript & DI

  • You need microservices or GraphQL


Conclusion

  • Node.js is the foundation for JavaScript backend development.

  • Express.js is great for quick, simple APIs.

  • NestJS is ideal for structured, scalable applications.


References

0
Subscribe to my newsletter

Read articles from Manish Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Agrawal
Manish Agrawal

Over 15 Years of Expertise in Software Development and Engineering I specialize in delivering innovative solutions across diverse programming languages, platforms, and architectures. 💡 Technical Expertise Backend: Node.js (Nest.js, Express.js), Java (Spring Boot), PHP (Laravel, CodeIgniter, YII, Phalcon, Symphony, CakePHP) Frontend: React, Angular, Vue, TypeScript, JavaScript, Bootstrap, Material design, Tailwind CMS: WordPress, MediaWiki, Moodle, Strapi Headless, Drupal, Magento, Joomla DevOps & Cloud: AWS, Azure, GCP, OpenShift, CI/CD, Docker, Kubernetes, Terraform, Ansible, GitHub Actions, Gitlab CI/CD, GitOps, Argo CD, Jenkins, Shell Scripting, Linux Observability & Monitoring: Datadog, Prometheus, Grafana, ELK Stack, PowerBI, Tableau Databases: MySQL, MariaDB, MongoDB, PostgreSQL, Elasticsearch Caching: Redis, Mamcachad Data Engineering & Streaming: Apache NiFi, Apache Flink, Kafka, RabbitMQ API Design: REST, gRPC, GraphQL Principles & Practices: SOLID, DRY, KISS, TDD Architectural Patterns: Microservices, Monolithic, Microfronend, Event-Driven, Serverless, OOPs Design Patterns: Singleton, Factory, Observer, Repository, Service Mesh, Sidecar Pattern Project Management: Agile, JIRA, Confluence, MS Excel Testing & Quality: Postman, Jest, SonarQube, Cucumber Architectural Tools: Draw.io, Lucid, Excalidraw 👥 Versatile Professional From small-scale projects to enterprise-grade solutions, I have excelled both as an individual contributor and as part of dynamic teams. 🎯 Lifelong Learner Beyond work, I’m deeply committed to personal and professional growth, dedicating my spare time to exploring new technologies. 🔍 Passionate about Research & Product Improvement & Reverse Engineering I’m dedicated to exploring and enhancing existing products, always ready to take on challenges to identify root causes and implement effective solutions. 🧠 Adaptable & Tech-Driven I thrive in dynamic environments and am always eager to adapt and work with new and emerging technologies. 🌱 Work Culture I Value I thrive in environments that foster autonomy, respect, and innovation — free from micromanagement, unnecessary bureaucracy. I value clear communication, open collaboration, self organizing teams,appreciation, rewards and continuous learning. 🧠 Core Belief I believe every problem has a solution—and every solution uncovers new challenges to grow from. 🌟 Let's connect to collaborate, innovate, and build something extraordinary together!