062 Creating and Mounting Multiple Routers

IT Services & Consulting
1 Oct 202008:04

Summary

TLDRThe instructor explains how to modularize a Node.js application by separating routes and handlers into different files for each resource. First, create a router for each resource using Express's router() method. Mount these routers onto specific app routes using app.use(). With requests matching mounted paths hitting resource routers first, routes can now be simplified to their base or id paths. The instructor demonstrates this by creating tour and user routers, mounting them, and simplifying routes accordingly. The application structure is now ready to have routers and handlers separated across files in the next lecture.

Takeaways

  • πŸ˜€ The goal is to separate the route handlers into multiple files - one for tours, one for users, etc.
  • πŸ‘¨β€πŸ’» Create a new router for each resource using Express's router() method
  • πŸ—ΊοΈ Mount new routers onto specific routes using app.use() - this makes sub-applications
  • 🌎 The mounted route becomes the root for that sub-application router
  • 🚧 Change mounted router paths to be relative to mounted route not full app route
  • πŸ“‚ Router mounting enables separating code into different files by resource
  • πŸ”€ Requests enter main app, get routed to sub-app router if route matches
  • ☝️ Must declare router variables before mounting them with app.use()
  • βœ… Test changes to ensure routes and sub-apps still work as expected
  • πŸ“ Main app handles overall routing but delegates to mounted sub-app routers

Q & A

  • What is the ultimate goal when separating the code into multiple files?

    -The goal is to have one file that contains all the routes, another file with the user routes, another with the tour routes, one file for the user handlers, and one file for the tour handlers.

  • What do we need to do before we can separate the routes into different files?

    -We need to create one separate router for each of our resources, like the tour router and user router that were created.

  • How do we connect the new routers to our main application?

    -We use the routers as middleware with app.use(), specifying the router and the route we want to mount it on.

  • Why do the routes in the routers only need '/' and '/:id' now?

    -The main route is already specified when mounting the router middleware. So '/' in the router refers to the root of that mounted route.

  • What is mounting a router?

    -Mounting a router means attaching a router as middleware to a specific route path in our main app. This creates a sub-application that handles requests to that path.

  • How are requests handled with the mounted routers?

    -Requests go into the middleware stack. When they match the mounted path, the router middleware handles it and its routes are checked before calling the route handlers.

  • Why do we mount the routers after declaring them?

    -The routers need to be declared before mounting so that they are defined when our app tries to use them as middleware.

  • Do the changes with routers affect the functioning of existing routes?

    -No, testing the routes shows that everything still works correctly after refactoring to use mounted routers.

  • What files will the routes be separated into?

    -The plan is to have one file for the tour routes, one for user routes, one for tour handlers, and one for user handlers.

  • What is the next step after mounting the routers?

    -The next step is to actually separate the router and handler code into the individual files for tours and users.

Outlines

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Mindmap

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Keywords

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Highlights

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now

Transcripts

plate

This section is available to paid users only. Please upgrade to access this part.

Upgrade Now