Title: Embracing NestJS: A Powerful Alternative to Spring Boot
Written on
Chapter 1: Introduction to NestJS
If you appreciate the features of Spring Boot, you might find NestJS equally appealing. By merging the advantages of Node.js and TypeScript, NestJS offers a strong framework that enhances development efficiency.
I've been working with Node.js for over four years, initially sticking with Express. However, as I embarked on various projects, I realized that Express often required substantial initial setup. Essential features like CORS and body-parser are not included by default and necessitate the installation of additional packages.
Through my learning endeavors, I encountered Spring Boot, which I found to be incredibly efficient and user-friendly, significantly accelerating my development workflow. This led me to search for a comparable "JavaScript Spring Boot," and I eventually discovered NestJS!
NestJS is a framework built on TypeScript and Node.js, essentially acting as a wrapper around Express. It provides complete MVC support and utilizes TypeScript decorators for module declarations, along with supporting dependency injection.
Section 1.1: Setting Up Your NestJS Project
Understanding that project setup can be daunting, the NestJS team created a command-line interface (CLI) that quickly generates a boilerplate project. To get started, simply run:
npx @nestjs/cli new my-nest-app
This command will set up a new project that’s ready to go. During installation, you can select your preferred dependency manager.
Once your project is ready, you can launch it with the command:
npm run start:dev
This will start your API on port 3000. You can verify it by visiting http://localhost:3000, where you should see a "Hello world!" message.
Subsection 1.1.1: Project Structure Overview
NestJS projects consist of three primary components:
- Modules: These connect various files and resources.
- Controllers: They handle API requests and define endpoints.
- Services: These classes manage business logic and application layers.
The app.module.ts file contains an empty class decorated with @Module(), which comprises three parameters:
- Imports: Used for linking modules together, with the main module responsible for importing others.
- Controllers: An array where all controller classes are declared.
- Providers: An array for injectable classes, primarily services.
The AppModule is referenced in the main.ts file, serving as the central module.
Section 1.2: Understanding Controllers and Services
Next, let's examine the app.controller.ts file. It features a class decorated with @Controller, allowing it to serve as a controller within the module.
In this setup, one route is defined with a GET method, specified by the @Get decorator. Unlike Express, NestJS simplifies the response process; you merely need to return the result.
Within the controller, we can inject the AppService class, which is possible because it’s declared in the module's providers array.
Finally, let's look at app.service.ts. The @Injectable decorator makes the class available for dependency injection. This service provides a method called getHello, which is utilized in the controller.
Chapter 2: Creating Your Own Module
Before creating a new module, let’s clean up the existing app. Remove app.controller.spec.ts, app.controller.ts, and app.service.ts as they will no longer be needed.
Don’t worry about potential errors; we’ll resolve them! Open app.module.ts and adjust it accordingly.
Now, create three new files:
- hello.module.ts
- hello.controller.ts
- hello.service.ts
Creating and linking our module starts with defining the HelloModule in hello.module.ts.
Next, add the HelloController to the controllers array in the new module.
In hello.controller.ts, create a controller and a GET endpoint. Initially, we’ll return a hardcoded string, but the ultimate goal is to connect it to a service for dynamic responses.
Finally, implement the service in hello.service.ts, which will return a string. Update your controller to utilize this service and check the results by reloading localhost:3000 to see the new service in action!
If you’re interested in the final code, it’s available on my GitHub repository.
I genuinely enjoy using NestJS, as it adds a layer of robustness to backend development through its MVC architecture and adherence to SOLID principles. If you found this article helpful, please consider following me or giving a clap!
More content is available at PlainEnglish.io. Sign up for our weekly newsletter, and connect with us on Twitter and LinkedIn. Join our Community Discord and be a part of our Talent Collective.
Explore the differences between Spring Boot and Node.js, and understand the strengths of each framework.
Learn about the distinctions between Spring, Spring Boot, and the Spring Framework.