Chapter 1

Getting Started with Nest.js

What is Nest.js

Managing a large-scale application can be tedious, especially when built without a well-planned structure and strict code organization strategy. Nest.js aids in this by enforcing modularity, thereby following the Single Responsibility Principle, a core tenet of solid software engineering.

Nest.js is a Node.js framework designed for crafting efficient, reliable, and scalable server-side applications. Built on top of Express and TypeScript, it adopts Node.js and Angular design patterns to provide a cohesive development experience. The framework’s architecture embraces decorators and dependency injection, patterns borrowed from Angular, to facilitate better code organization and reusability.

What Problem Nestjs Solves

Maintaining consistency across a large codebase can be difficult; Nest.js addresses this by incorporating software engineering design patterns like SOLID and Dependency Injection. These design patterns contribute to a clean, maintainable, and scalable code architecture, adhering to Nest.js’s principle of creating easily testable and loosely coupled code.

While unopinionated frameworks like Express offer multiple ways to structure your code, the freedom to choose can lead to decision paralysis, wasting time that could be better-spent building features. Nest.js sidesteps this issue by providing a modular approach right out of the box, allowing for flexible code organization while still enforcing a structured layout, aligning with its principle of modularity.

Sticking with an initial code organizational decision can be difficult, particularly as team members come and go. Nest.js’s structured, modular approach helps maintain architectural integrity over time, making it easier for new developers to understand the codebase quickly.

Essentially, the issue Nest aims to solve is creating a robust architecture for backend applications. By providing a well-defined structure and integrating established design patterns, it answers a pressing question many developers have about organizing architecture for enterprise or large server-side applications.

Why Nest.js was created

The creator of Nest.js drew inspiration from Angular’s design architecture to build a front-end application. Transferring this idea to the server side allowed for the broad utilization of a familiar architecture that incorporates Nest. js-specific elements like modules, dependency injection, providers, and custom decorators. This strategy embodies the Nest.js principle of modularity and extensibility, making it easier for developers who are already familiar with Angular to adopt Nest.js.

Why should you use Nest.js

If you aim to build server-side APIs utilizing TypeScript, Nest.js is the framework of choice. Nest.js employs strong typing and decorators, elements intrinsic to TypeScript, thus aligning closely with TypeScript principles. It also supports Dependency Injection out-of-the-box, encouraging a modular, scalable architecture.

Nest.js is particularly accessible for developers familiar with Angular, making the learning curve more manageable. Both frameworks share common programming paradigms and syntax, including the use of decorators and modules, enhancing code reusability and maintainability.

Nest.js enables you to construct various backend services, from RESTful APIs and GraphQL endpoints to MVC applications and WebSockets. The framework’s flexibility accommodates multiple communication patterns, allowing for an extensive range of backend solutions.

Designed for building both large-scale monolithic and microservices applications, Nest.js offers scalability and modular architecture. It employs a “Module” system to organize code, which allows for straightforward unit testing and easier collaboration.

Nest.js provides native integrations with a host of external tools like Mongoose for MongoDB, TypeORM for various databases including Postgres, and many more. This extensibility allows developers to incorporate a multitude of functionalities without boilerplate code, making the framework more versatile.

Benefits of using Nest.js

  • Utilize Angular-style syntax for the backend, a feature unique to Nest.js, that promotes consistency and reusability across the tech stack. This syntax allows developers familiar with Angular to seamlessly transition into backend development.

  • Leverage the detailed documentation with examples that Nest.js provides. This not only accelerates the learning curve but also aligns with Nest.js’s principle of being developer-friendly and easy to pick up.

  • Benefit from the framework’s focus on good architecture and fast development. Nest.js follows the modular architecture pattern, allowing for better separation of concerns and easier testing, which in turn speeds up the development process.

What type of Application can you build

  • You can build backend REST and GraphQL APIs using Nest.js, benefiting from its modular architecture that promotes code reusability and maintainability. The framework’s built-in support for these query languages facilitates quick API development.

  • You can build microservices with Nest.js, taking advantage of its inherent support for multiple message transport layers. This aligns with the microservices architecture principle, which advocates for loosely coupled, independently deployable components.

  • You can construct a backend for a streaming application using Nest.js, as the framework supports asynchronous data handling through Observables. Nest.js makes it easier to work with real-time data streams, thanks to its integration with libraries like RxJS.

  • You can build the backend of a real-time application in Nest.js, utilizing its WebSocket support for real-time two-way communication. This is essential for applications requiring instant data update and interaction, adhering to the principles of real-time system design.

Who is using Nest.js in production

  • Roche is a multinational healthcare company operating under two divisions, pharmaceuticals and diagnostics. This American biotechnology company uses the Nest.js framework on its main website to reliably cater to its patients and to further expand its services.

  • Adidas is the largest sportswear manufacturer in Europe and the second-largest in the world. Adidas is known for designing and manufacturing shoes, clothing, and accessories. Their global reach and popularity are why they decided to use the highly scalable Nest.js framework to build large-scale, efficient applications for their brand.

  • Decathlon is a sporting goods retailer with over 1,500 stores in 57 countries. Decathlon chose Nest.js as the backend for their web application to help them scale and maintain their legacy codebase.

 

Creating a Nestjs Project

Before creating the Nestjs project. Make sure you installed Node. js on your machine.

Steps to create Nest.js project

1. Install the nest-cli globally

2. npm install -g @nestjs/cli

Create a new project using Nest cli

You have successfully installed the nest cli. Now it is time to create a new project using the Nest cli command nest new [name of the project]

nest new n-fundamentals

Start the Project

The project with the n-fundamentals name will be created. You can choose any project name here. When the project is created successfully you can start the project by using npm run start:dev

The project will be running at http://localhost:3000

 

Project Directory structure

Nest.js designates the src folder as the location where you’ll place your application’s source code. This arrangement adheres to Nest.js’s modular architecture, promoting better organization and separation of concerns within your application.

Within the src folder, the main.ts file serves as the entry point of your Nest.js application. This file is responsible for bootstrapping the application, utilizing Nest.js’s core function NestFactory to create an instance of your Nest application.

import  {  NestFactory  }  from  "@nestjs/core";
import  {  AppModule  }  from  "./app.module";
async  function  bootstrap()  {
const  app  =  await  NestFactory.create(AppModule);
await  app.listen(3000);}bootstrap();

 

The main.ts file serves as the entry point for the application and employs the core function NestFactory to instantiate a Nest application. In Nest.js, NestFactory is pivotal for bootstrapping the application, setting up the dependency injection system, and initializing modules. This follows the Nest.js principle of modular development and centralized configuration.

  •  app.controller.ts: This is a basic controller file containing a single route. Controllers in Nest.js are responsible for request handling and response sending, acting as a gateway between client and server.

  •  app.controller.spec.ts: This file contains unit tests for the controller, adhering to the Nest.js focus on test-driven development (TDD).

  •  app.module.ts: This is the root module of the application, which imports other modules and providers. Nest.js modules act as organizational units and follow the Single Responsibility Principle.

  •  app.service.ts: A basic service file with a single method. In Nest.js, services encapsulate business logic and can be injected into controllers, promoting Dependency Injection and the Separation of Concerns.

  •  nest-cli.json: Utilized for Nest.js-specific configurations, this file allows customization of compiler options, assets, and other settings.

  • .prettierrc: This file is used for configuring Prettier, aiding in code formatting and style consistency within the Nest.js project.

  • tsconfig.json: This configuration file is for TypeScript and determines how the TypeScript compiler will behave. This aligns with Nest.js’s use of TypeScript for strong typing and better code quality.

$7 bundle of my best NestJS + backend developer Courses.


More details coming soon.

Get the latest insights from the marketing world.

A blog that focuses on providing practical tips and strategies for businesses to improve their marketing and sales efforts.

Solutions

Helping you help your customers.

Sell smarter, better, and faster.

The insights you need to make smarter business decisions.