-
Notifications
You must be signed in to change notification settings - Fork 0
modules
With this chapter, if you want to follow along, check out the modules-start
branch and go to the following along section.
If you just want a final version of the project as of the end of the chapter, check out the modules-end
branch, and proceed to the Upon completion section.
The main point of the chapter is to learn to create modules. I recommend creating the cats
module with the CLI, as described here in the docs to get a feeling for how it works. Here's how.
If you started out on the modules-start
branch, run the CLI command as shown in the Feature modules section. Because we've been following good naming conventions (putting our cats-related code in the src/cats
folder), Nest will do the smart thing and put our new cats module right in that folder!
As shown in the docs, use the CLI as follows to generate the module:
nest g mo cats
What this CLI method won't do, since we are creating the Cats
module after we already have created service and controller classes (in the previous chapter), is register the metadata for these already-existing classes in the module*. We'll need to do that manually. To do that, make sure the src/cats/cats.module.ts
file looks like the code shown in the docs, here.
*For future reference, if you first create a module, then add services, controllers, etc., Nest will automatically "wire them together" with the appropriate ES6 imports and Nest Module imports.
On the other hand, note that the CLI method did, in fact, import the brand new Cats
module into the root module (AppModule
) in src/app.module.ts
. 😍
The rest of the chapter illustrates a number of points, but does not contribute code to "nest cats mainlne". You should read through those sections, and then return here when you're done.
We've mainly just added some module structure in this chapter. You can now test the app with the same REST API requests we used earlier (see below). As before, make sure the app is running with npm run start
or npm run start:dev
(recommended).
Use the following HTTPie queries to verify that the app still functions now that we've organized the cats-related features into their own module.
Create a cat
http POST :3000/cats name=Fred age:=3 breed='Alley Cat'
Get cats
http GET :3000/cats
This chapter explores some advanced features, particularly Dynamic Modules. We'll cover those in greater detail in a subsequent chapter/guide section.
Next up is the Middleware chapter.