Creating your first NestJS application

Photo by Sereja Ris on Unsplash

What’s NestJS?

NestJS is a framework for a fast development of backend applications whose structure, inspired from Angular, makes it perfect for the realization of projects with MEAN stack.

When to use NestJS?

Nest is most suitable for developing applications that manage a huge quantity of requests with a tiny amount of data processed on the server side and prompt response time (e-commerce, live chats, etc..).

Why use NestJS?

The benefits of using NestJS are innumerable. The most important (imho) to highlight are:

  • Type checking: with Typescript we exploit all the power of javascript without running into type problems.
  • Modular: through the division into modules we can isolate our components based on the domain boundary.
  • Dependency Injection: thanks to the DI container we are able to write decoupled and testable code.
  • Adapters: Nest integrates all the most used and community tested javascript / typescript libraries and provides all the tools developers need to contribute to the collection.
  • Angular Like: the familiar structure allows us to easily switch from frontend to backend without having to change the development approach every time.
  • CLI: we can develop our functionality without having to worry about the initial boilerplate.

Let’s start writing code

  • retrieve the list of all todos
  • retrieve a single todo
  • insert a todo
  • edit a todo
  • delete a todo
  • [GET] todos
  • [GET] todos/{id}
  • [POST] todos
  • [PUT] todos/{id}
  • [DELETE] todos/{id}
Ehm 😅

step 0: Setup

Setting up our project with two simple commands in terminal:

npm i -g @nestjs/cli
nest new TodoBackend
NestJS bootstrap logs

step 1: Module

Modules are the mechanism that allows us to separate our components based on a domain they belong to and, in practice, to instruct the DI container about their interactions in the bootstrap phase.

  • It’s not a typical case and consequently difficult to apply in a real context.
  • We would not be decoupled, thus losing the maintainability and portability of our code.
nest generate module todo

step 2: Entity

An entity is a class that maps a table (or collection) of our database (very pragmatic definition).

nest generate class todo/entities/Todo --no-spec

step 3: Repository

Now that we have our entity we just have to persist it through an ORM!

npm i @nestjs/typeorm typeorm sqlite3

step 4: DTO

To avoid exposing our entities outside our business logic layer, we define a set of classes that will be used to manage the communication in and out of our services: the DTO (Data Transfer Objects).

step 5: Service

The service is the “package” where we are going to encapsulate our business logic, exposing a set of ready-to-use features.

nest generate service todo/services/todo
nest generate service todo/services/TodoMapper

step 6: Controller

Here we are at the last layer of our climb to the NestJS stack! ⛰

nest generate controller todo/controllers/todo
@Post()
@UseInterceptors(ClassSerializerInterceptor)
public add(@Body() todo: AddTodoDto): Promise<TodoDto> {

step 7: Validation

Our DTOs are ready to travel as fast as Formula 1 cars under the http protocol but one last piece is missing: the validation of its data.

npm i class-transformer class-validator

step 8: Run!

It’s time to run our application! 🎉

npm run start
npm run start:debug

step 9: Enable CORS

After developing our backend, we will most likely want to develop a frontend application and try to consume our todo-backend, here the CORS comes into play:

step 10: Test

Tests are absolutely necessary when you want to build a maintainable application: they prevent us from the most common development errors and possible regressions due to the modification of an existing functionality.

Conclusion

By creating the application described in this article, I was able to see firsthand how fast and powerful this framework is: it allows you to be extremely fast and flexible without having to give up absolutely anything. NestJS passes exams with top votes and can deservedly occupy its place in the top list of frameworks for web development.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store