DEV Community

Daniel Engelhardt
Daniel Engelhardt

Posted on • Originally published at hardnold.hashnode.dev on

Nest.js: Why it is a Game-Changer for me in Node.js Development

A couple of years ago I stumbled upon Nest.js, found it interesting and then - forgot about it. Until recently when I was going down a rabbit hole about clean code architecture in Node.js. Nest.js came across quiet often and so I decided to take a closer look and oh boy am I excited about it. I can't believe I didn't go deeper when I first heard about it. If you're into crafting efficient, scalable server-side applications, you're going to love this!

So, what is Nest.js?

At its core, Nest.js is a framework for building efficient, reliable, and scalable server-side applications. It's designed with the principles of Object-Oriented Programming, Functional Programming, and Functional Reactive Programming in mind. This unique blend makes it incredibly powerful and flexible.

TypeScript - The Heart of Nest.js

Nest.js is built with and fully supports TypeScript, a language that builds on JavaScript by adding static type definitions. TypeScripts popularity has skyrocketed in the last years, thanks to its ability to catch errors early and its support for advanced JavaScript features. Nest.js leverages this power to the fullest, allowing developers to write more reliable and maintainable code.

Inspired by Angular

One of the most intriguing aspects of Nest.js is its architectural similarity to Angular. This is no coincidence, as the creators of Nest.js designed it to provide an Angular-like experience on the server side. This means if you're familiar with Angular, you'll feel right at home with Nest.js. It adopts many of the same concepts, such as modules, services, and dependency injection, ensuring a consistent and intuitive development experience across your full-stack applications.

Emphasis on Testability

Nest.js places a strong emphasis on testability. Its architecture is designed in a way that makes it straightforward to write and execute tests. This is a crucial aspect of modern software development, as it ensures that your application is reliable and maintainable in the long run.

Rich Ecosystem

Nest.js comes with an impressive ecosystem. It has a comprehensive collection of libraries and tools that make it easy to integrate with databases, queue systems, search engines, and more. This rich ecosystem means you spend less time worrying about compatibility and more time building great features.

Community and Support

Finally, the community around Nest.js is vibrant and growing. With an active GitHub repository, a bustling Discord channel, and numerous online resources, finding help, discussing best practices, or simply staying up-to-date with the latest features is easy and accessible.

Nest.js vs plain Express.js-Apps - A comparison

Scenario 1: Simple App

Alright, let's finally get our hands dirty with some code, shall we? We'll first compare a basic REST API endpoint created with plain Express.js and then with Nest.js:

Express.js-Version

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

app.get('/users', (req, res) => {
   res.send('List of users');
});

app.listen(port, () => {
   console.log(`App listening at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Simple, neat and pretty straight forward, right? But as the application grows, things can get messy with Express.js. You might end up with a spaghetti code situation if you're not careful.

Nest.js Version

Now, let's look at how Nest.js handles the same functionality:

import { Controller, Get, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

@Controller('users')
class UsersController {
  @Get()
  findAll(): string {
    return 'List of users';
  }
}

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

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

A bit more code, but look at that structure! 🤩 Each part has its own place, making it more maintainable and scalable.

Now, let's go even further and add some business logic.

Scenario 2: User management with additional services

We'll create a user management system. In Express.js, integrating additional services like logging or authentication often leads to more complex and intertwined code. In Nest.js, these can be neatly separated due to its modular and controller-based architecture.

Express.js Example: Complex and Intertwined Code

Let's start with the plain Express.js example. In this example, we're handling user operations along with logging. Notice how the logging logic is mixed with the business logic, making the code less maintainable.

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

app.use(express.json());

const logRequest = (req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
};

app.get('/users', logRequest, (req, res) => {
  // Business logic to fetch users
  res.json({ users: ['Alice', 'Bob'] });
});

app.post('/users', logRequest, (req, res) => {
  // Business logic to create a user
  res.status(201).send({ user: 'New User' });
});

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Nest.js Example: Separation of Concerns and Modularity

Now, let's see how Nest.js can help organize the same functionality more effectively:

import { Controller, Get, Post, Module, Injectable, NestMiddleware, MiddlewareConsumer } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

@Injectable()
class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: Function) {
    console.log(`${req.method} ${req.url}`);
    next();
  }
}

@Controller('users')
class UsersController {
  @Get()
  getAllUsers() {
    // Business logic to fetch users
    return { users: ['Alice', 'Bob'] };
  }

  @Post()
  createUser() {
    // Business logic to create a user
    return { user: 'New User' };
  }
}

@Module({
  controllers: [UsersController],
})
class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes(UsersController);
  }
}

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

In this Nest.js version, we've created a LoggerMiddleware that is responsible solely for logging. It's applied to the UsersController routes in a clean and non-intrusive way. The UsersController focuses purely on handling user-related requests.

Key Takeaways

  • Separation of Concerns : Nest.js excels in separating different concerns of the application (like logging and business logic), which is crucial for maintaining large-scale applications.

  • Modularity : Nest.jss module system allows you to organize your code in a modular fashion, making it more readable and easier to manage.

  • Cleaner Middleware Integration : Middleware in Nest.js can be applied in a more structured and less intrusive way compared to Express.js.

Wrapping Up

Nest.js represents a significant step forward in the world of Node.js development. Its combination of a robust architecture, TypeScript support, and a familiar Angular-like structure makes it a compelling choice for developers looking to build scalable and maintainable server-side applications.

Whether youre an experienced Node.js developer or just starting out, Nest.js offers a powerful toolkit to elevate your development experience. So, get ready to explore this fantastic framework and see how it can transform your projects!


I'm just starting blogging about things that come along my way and spark some interest in my brain. Let me know what you think about this post.

Top comments (0)