NestJS Tutorial #1 How to create and organize modules in NestJS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome back to the new tutorial of the nest CHS today we are going to learn about how to create and organize modules in the nest CHS those who haven't subscribed my channel yet please do subscribe my channel and if you like the video please to like share and comment on my video so let's get started so how we are going to create a module we are going to learn in this video so to create a module you have to use Nest generate module and the module name okay this is the command so whatever module name that is the user module or or you can say about the O module so Nest generate module and the module name this command creates a new module file what is a module structure inside the generated module file module name module. TS a basic module structure looks like this so this is basically you can see we have a Imports we have a controllers we have a providers and the exports okay so this is a basic module structure and uh organizing modules what exactly it is feature based modules divide your application into the feature based modules where each module represents a distinct feature or functionality so you can organize modules with the help of the feature based suppose you have a user module or module product module or the order module so you can divide with the help of the features and organize the modules like this shared modules you have a modules with the shared one create a shared modules for commonly used function fun alties that multiple feature modules might needed for instance shared module contains reusable components services and utilities okay next we have a dependency injection utilize dependency injection by importing modules into each other to assess their exported functionalities import required modules into the Imports array of the module decorator use providers and the controllers from other modules by adding them to the provider ERS and controllers array respectively okay then we have a exports exports necessary components Services controllers ET from a module using the exports array in the module decorator this allows these components to be used in other modules if the module is imported there so basically these things are basically over here you can see exports right so here we have first we have a import s other modules that this module depends on then we have a controllers controllers within this module providers services and other providers within this module then we have exports okay so this is the module that we have currently discussed right let's take an example and let's come to know about this one okay so this is basically the example there you can see that this is a user module. TS file okay so user module is there then we have a controller so you can see this is a controller file so we we have used this controller over here and this is the import from the user controller right then we have a providers that is services so user Services we are using then exports export user services from the other modules so here you can see in exports they have used export user services from the other modules okay then export this user module and the main one suppose we have a o module. TS file so again here you can see that in the Imports we have a user module import user module to the user services for the because user module basically in which one user module to use the user Services okay then we have a controllers then we have a providers that is all service from the all service part okay so this is the basic structure of that one right so let's create that one with the help of an example okay so let's go step by step what exactly we are doing doing so basically first we are going to create a nest application and let's go with this one so this is basically theoretical things now we are going to practical one so let's go to the terminal so first we create the nest module new application new Nest module example okay then CD this one for okay so we go with npm and if I go to the folder one so here it's coming up over here nextest module so you can see the structure is completed but the node module are still pending so it's installing the non modules for almost I think it will take seconds to now installation in progress installation completed okay next p go to that folder okay to generate a new module what we have the command next generate module cat so I'm going to generate a cat module over here so here we go with this one okay so it automatically created the files over here okay then we have to create a new module that is dog module so with the help of same we have to generate this okay let's open the code base also okay so in the source we have the cat module then we have a dog module okay those modules are there okay so next we have a create controllers and the services okay to generate the cat controller and the cat services inside the cat module okay so here we go with this one so first we create the uh controller with the help of this okay so cat controller and Cat module updated next we have to generate the services so here we should go with the services is created here you can see that one so in the cat module we have a cat controller cat module is there right in the dog dog we are just creating and here you can see that cat services is there right okay then so the next one we have to generate the same for the dog one okay then we have to go with the services okay so services and the controllers are created now implement the functionality in the controllers and the services so first one is cat service so if I open the cat service over here here you can see basic file structure is there right so let's add our code over here okay so we have a cats array and get all cats this do cats and add cat we will push the cat name into it so it accepts the parameter name okay next one we have a controller for the cat one so here we go with this one so here we have all the logic building over here so here we have the cat controller okay so here we have to go with the cat service right so this is cat service and the cat services we are use this do cat services here this is passed in the Constructor dependency injection is there right so this do cat service. getet all cats and here it it resides in service section where it return all the cats right so this is the structure that the nest is following right so the next one we have to go with the dog module then we have a dog service same thing that we have to do with the dog module also dog service okay then we have a do controller dog controller is there okay and here just need to pass this one okay so this is get one this is post one okay after this we have a module configuration so we have to incl include that one so this is cat module so let's see the cat module what exactly it is so here we have a controller so I think we should go with this so it's there controllers that we have used service it's spending so import that service also in that one so we need the services okay so here the service is import now in the controllers is there then we have a providers okay so we have to use the providers over here okay cat services and Export the cat services for the potential use in other modules so if I want to use in the other modules so I need to export that one over here export cash service for potential to use in the other modules if I want to use right then export class default cat module then we have a dog module okay so Services we need to import and let's go with the providers and the exports okay export doc service for potential use in the other modules save this okay the last file that is app module. so this is the entry point of every module so it should include the main modules of cat module and the dog module that should be in the import part okay so let's see the last file is app module dots and already mentioned the cat services and the dog services over here okay so here you can see that the cat module and the dog module is already cat module and the dog module is already added and the providers Services app Services cat services and the dog services already added otherwise we because cat services we already added into our this module just give me a second where we have a cat module you can see the providers cat services we already added if you want to skip that one you can we can skip this also okay only we go with the app Services because already mentioned in the cat module right so this is done let's try to run this pro project npm Run start T okay so here you can see we have the routes coming over here that is cats that is get request then the cats for the Post request so here you need to uh let's create one of them so let's 3,000 we have a cats make a post request then we require I think raw we will send the uh body I name is there test cat added successfully and there should be a cat API that is get let's add the cat one over here you can see I think it what is the get request is there it's cats okay so let's make it cats and check test is there right so because we have pushed that data and successfully we get this one in the cat section okay so this is how the modules work okay the modu modular structure of that one so easy and very simple so whenever you have such kind of functionality then you can you have to use that you have to follow that structure okay any doubt any query in that do let me know in the comment section thank you so much for watching this video have a great day
Info
Channel: Technical Rajni
Views: 73
Rating: undefined out of 5
Keywords: nestjs tutorial, nest.js tutorial, tutorial, nest tutorial, nest js tutorial, nestjs tutorial for beginners, node tutorial, node js tutorial, best nestjs tutorial, nest.js tutorial for beginners, next js full tutorial, jest tutorial, best tutorial, programming tutorials, nest tutorial 2021, nestjs tutorials, next js tutorial, nodejs tutorial, nestjs full tutorial, nest.js full tutorial, nestjs testing tutorial, microservices tutorial, nestjs postgres tutorial
Id: TG6aH-EzUGM
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Sat Dec 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.