Nest.js Crash Course #2 – Modules

In this series you’ll learn how to make a ninja-themed API with Nest.js, which is a node.js framework for making server side applications.

⭐ Thanks to Marius for making this course. Subscribe to his channel here –

?‍? Nest.js docs –
?‍? VS Code –

So we mentioned in the first video that NASA is really sort of an architecture Out of the box that's meant to be used As an abstraction above something like Express and this architecture is made up Of a couple core pieces that we'll talk About across the series and let's start With modules you can see that we have an App module that has your controllers and Providers You can think of this app module as the Root of our application and we can Introduce additional modules to our Application now take a modules as a way To group and encapsulate a closely Related set of capabilities and imagine That you know most projects your modules Will probably be for each feature and Each of those features might have their Own routes you know their own business Logic so for example maybe you'd have a Module for users you know for managing Who are the users of your application be Able to create read update delete users If you were doing some kind of E-commerce app maybe you'll have you Know an orders module or a products Module so ultimately your application Becomes composed of all these modules And each of these modules will have some Kind of dependency on each other right So everything pretty much starts at the App module as the root but modules can Also depend on other modules for example

An orders module maybe it depends on a Products module and so on right so Ultimately what you end up with is some Kind of tree structure that always has To have at least one root module So back in our code let's go ahead and Create a new module to add to our Application and remember in the first Video we installed a net CLI you can Actually use that to generate a lot of The new files for you and it'll help Wire things up easily so you can use the Command Nest generate Module and then give it the name of the Resource that you're trying to create in This case where let's call it ninjas Because we're trying to create a ninjas Module You can also use Nest G for short which Is the same thing And if you pay attention to the output Here you'll see that it says it created A new file ninjas module and it also Created an update in our app module Notice that there is now a new Imports Here of the Ninjas module so it's Actually starting to build that Dependency tree for us automatically so That's sort of lesson number one is as You're creating new modules make sure That it's being added to the Imports Array of another module and that's Effectively how things are registered in SGS same thing for controllers and

Providers as we you know create those We're going to register them to the Module that it belongs to so a natural Next step for us is to add to our ninjas Module and add you know a new controller And a new service for this now similarly You can use Nest G Controller ninjas So similarly you can see that a new Controller file was created and it also Updated our ninjas module to register That controller Now let's also do Nest G's service back In the terminal down here ninjas because We want to add a provider Again same thing new files updated Module and you can kind of see some of The benefits of the CLI here where you Know it starts to inform you of what's a Recommended folder structure what's a Recommended file naming convention you Can of course do all these manually if You wanted to but it can save you a lot Of time in terms of you know generating Files and hooking things up now I should Mention that there is also a CLI command That allows you to generate an entire Resource along with you know controllers And providers all in one command so for Example you can do an SG resource and Provide that name of your resource or Maybe let's do something different such As users and then you can also specify The dry run command if you'd like but if

I run this you'll see that it'll ask you What transport layer do you use you can You know pick rest API graphql let's Take a look at rest API and then I'll Hit yes here for the one to generate Crowd endpoints and you'll notice that When I hit yes here it'll create quite a Bit more of those files it'll have the Controller it'll have the service but a Bunch of other things such as details And entities You can see in the generated users Controller that it generated you know All of the crowd endpoints great read Update delete as well as the underlying Service methods for those now I would Only use that resource command if you're Already fully aware of the fundamentals Of Nest you know otherwise it's going to Spit out a bunch of boilerplate for you That you might not be familiar with so In the following videos I'll walk you Through you know explaining to you what All of these little things are what are What do these decorators do and how to Set it up from scratch if you needed to But once you do have those fundamentals You know that's a CLI command that you Can use to get things up and running Very quickly creating a bunch of files And hooking things up for you Now in the next video we're going to Cover uh the controller how to add you Know new custom routes with differing

HTTP methods and how to respond to Requests so make sure to head on over to The next video and I'll see you there

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *