DEV Community

Cover image for Nestjs: Introduction and setup

Posted on

Nestjs: Introduction and setup

Nestjs is a Nodejs framework to build complex backend applications using Typescript instead of javascript for better code reliablity and less bugs.

Why use Nestjs

So why should you use Nestjs instead of just using express ? the answer is that Nestjs is kinda more opinionated on how you structure your application code, meanwhile express let's you freely organize the app howerver you want and that may lead to bad structure especially when your application grows in size and complexity, also Nestjs provides support for database connection, authentication, caching, graphql, testing and many other things out of the box.
also, keep in mind that Nestjs uses express behind the scenes but you can change it to fastify which is another good nodejs framework, so why not put the time to learn Nestjs ?

How a Nestjs app is structured

Nestjs applications are divided into modules, each module takes care of 1 feature of that application.
let's take an Ecommerce app as an example and divide it to Nests modules it would look something like this:

App diagram
and each module contains:

  • a controller where you define the different routes for that feature

  • a service that contains all the business logic related to that feature

  • guards to prevent access for a certain route until a condition is met (authentication for example)

  • a repository to access the database

  • data transfer objects to ensure the data coming from the client is valid

  • pipes to transform data

Getting started with Nestjs

First you need install Nodejs on your sistem, then install the Nestjs cli globally

npm install -g @nestjs/cli
Enter fullscreen mode Exit fullscreen mode

then create a new project

nest new project-name
Enter fullscreen mode Exit fullscreen mode

now you can open your editor and start coding the app

Top comments (0)