Why use NestJS? 5 Great Features!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll share with you five great reasons i think the nest js framework for node is a great choice for developing web applications in node in 2021 and beyond welcome along my name is chris and i'm a freelance software engineer from the uk i've been working with node and express to build web apps since about 2016. it wasn't until 2020 though when i discovered the nest framework that my enjoyment of developing with node and my development workflow really really hit their stride now there are lots and lots of things that i enjoy about working with nest but to keep this video fairly short and to the point i've chosen just five reasons i think it's great i think you will too now the first feature i really like is modules working with node and express on their own is great for building small lightweight apps where the code is nice and easy to read however as things start to get more complex and you add more features to your application then things start to get a little bit more stressy and hard to manage you'll have lots of different controllers in different folders each with their own sets of models and services and things can get quite complex quite quickly and your code can go from being nice and easy to maintain and read to being something more like spaghetti and that's where nests modules come in nest can organize your application into self-contained modules each with their own responsibility now into a module you can put related controllers models and services and have them fairly well isolated from the rest of your application in fact if you have a provider or service within a module and it's not explicitly exported from your module you can't actually access it in other parts of the application now this enforces code isolation single responsibility and it makes it easier to grow and extend a project across a team and what's more when your code is nice and easy to maintain and read it can make for a much more relaxed mind the second great feature of nests that i really like is dependency injection nest supports dependency injection right out of the box now you don't have to use it but it gears you up to use it so simply that it'd be crazy not to now what's great about dependency injection is it means you don't have to have hard dependencies on things like components services and middleware within your code that's really simple to use dependency injection in nest say for example you have a controller and you want to get access to a service that reads and writes from a database as long as your service is marked as injectable then simply reference it in the constructor of your controller and you'll have an instance of it now one great bonus feature of dependency injection in nest is something called injection scopes by default services in nest are singletons they're instantiated once and that's it each request access the same instance of a service however if you come from a background like asp.net where you're used to having your constructor called and your service is injected for each request then injection scopes allow you to have that behavior so instead of having one singleton for the whole application you can use injection scopes to get a new instance of that service for each request the third great feature of nest is exception filters all applications encounter exceptions from time to time and sometimes you might want to throw them on purpose how you handle exceptions is really important and what's great about nest is it sorts all that out for you in a web app you might throw an exception for example when a user isn't authorized or a resource is not found and this contains a number of built-in classes that wrap common http status codes that you can throw from anywhere in your application nest exception filter will pick them up and will return a nicely formatted response to your user including an appropriate http status code for example if your request body didn't pass validation you can throw a bad request exception and pass in your validation errors nest will then return a 400 error on your behalf and a nicely formatted json response with your validation errors now there are times of course in a web bat when things go wrong that you don't expect and there might be an unhandled or unexpected exception thrown nest traps sees two for you automatically and sends a nice 500 error to the user on your behalf and locks the exception to standard out if you want more control over how your exceptions are handled you can write custom exception filters this way you can add your own logging or your own status codes or even create custom exceptions throw them from anywhere within your application and nest exception layer will handle them all for you the fourth great feature of nest is super easy support a lot of web apps built with node are mean stack apps the m of course standing for and mongoose being one of the most popular libraries for accessing a database mongoose is a first class citizen in nest it's not quite out of the box you need to install a first party package but when you do mongoose support is very very simple all you need to do is import a mongoose module into your application and set the connection string and nest will handle the rest for you it will handle connection to the database on start and even handle retries if it can't establish a connection now nest has a lot of support for creating and managing mongoose models and schemas once they're created you can just pass them around and inject them into your controllers and into your services like any other injectable component and it's really as easy as that and that's one of the reasons i really enjoy nest because it makes supporting databases really really easy now the fifth reason that i really really like nest is it supports express now nest isn't built on express per se but it's out of the box configuration is to use the express framework as its request processing pipeline what this means is that if you're already familiar with the express request processing you'll feel right at home in nest in fact you'll be able to easily adapt your express middleware for use within nest and that's exactly what i did before starting using nest i built up quite a large library of express custom middleware and it didn't take much work to get this working within nest this is because in each request you can access the express request and response objects and manipulate them as you would within an express application however that being said i rarely use the express response object anymore because nest is just really good at handling my responses for me now if express isn't your thing then nest also supports fastify out of the box and you can also add support for other request processing frameworks using custom adapters now a bonus feature of nest which isn't really a feature but it's still worth mentioning is that nest is type script you don't have to configure your ts config or anything like that it just works so there's five reasons why i really like using nest as my development framework of choice for node.js do you use nest what are your favorite features let me know in the comments i'd left a link to the nest home page and the documentation in the description if you've not tried out please do i think you'll really like it if you like this video drop me a like if you'd like to see more content like this in the future drop me a subscribe too and hit the bell icon so you get notified when i upload videos and i'll see you in the next video [Music] you
Info
Channel: Roberts Dev Talk
Views: 18,778
Rating: undefined out of 5
Keywords: nestjs, nest js, nestjs vs express, nestjs dependency injection, nestjs mongoose, nestjs modules, nestjs exception filter, nestjs exception handler, why use nestjs, nestjs vs express js, nestjs express, node js, nodejs, Developer, Developers, Nest
Id: 4ZW9LQvUs9k
Channel Id: undefined
Length: 6min 48sec (408 seconds)
Published: Wed Jun 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.