Your Course Progress

Topics
0 / 0
0.00%
Practice Tests
0 / 0
0.00%
Tests
0 / 0
0.00%
Assignments
0 / 0
0.00%
Content
0 / 0
0.00%
% Completed

Expressjs Routers

Expressjs Routers are a powerful feature that allows you to organize your application's routes (URLs) and their corresponding handlers (functions that respond to requests). This approach promotes code organization and maintainability, especially as your application grows.

Here's a breakdown of how routers work:

  1. **Route Definition:** You create router instances in your application to define specific routes.
  2. **Handler Functions:** Each route is associated with a handler function that executes when the corresponding URL is requested.
  3. **Request Handling:** When a request arrives at your Express.js application, the routers examine the request's URL to determine the appropriate handler.
  4. **Response Generation:** The handler function processes the request and generates a response to be sent back to the client.

To create a router, you use the express.Router() function:

const express = require('express');
const router = express.Router();

This creates a new router instance. You can then define your routes using methods like get, post, put, delete, and more.

Here's an example of defining a route that handles GET requests to the path /users:

router.get('/users', (req, res) => {
  res.send('List of users');
});

This route will send the text 'List of users' as a response when a client makes a GET request to the /users URL.

After creating your routers, you need to mount them onto your main Express.js application. This tells Express.js to use these routers to handle requests for specific URLs.

const app = express();

app.use('/api', router); // Mount the router at the /api path

Now, any request that starts with /api will be handled by the router. You can mount multiple routers at different paths to organize your application's routes.

Important Note

You can also use app.use(router); to mount a router at the root path. In this case, all requests will be handled by that router.

You can even create nested routers to further organize your routes. For example, you could have a router for /users and within that, another router for specific user actions:

const usersRouter = express.Router(); // Router for /users
const userRouter = express.Router(); // Router for individual users

// User actions
userRouter.get('/', (req, res) => {
  res.send('User profile');
});
userRouter.put('/', (req, res) => {
  res.send('Update user');
});

// Users
usersRouter.get('/', (req, res) => {
  res.send('List of users');
});
usersRouter.get('/:userId', userRouter); // Mount the userRouter for specific user

// Main app
app.use('/users', usersRouter); // Mount the usersRouter

This nested structure helps in creating a more modular and maintainable codebase. When a request comes in like /users/123, the usersRouter will be invoked first. Then, it will forward the request to the userRouter (for individual user actions) since the path matches /:userId.

  • Routers in Express.js help organize routes and handlers.
  • Use express.Router() to create routers.
  • Define routes using methods like get, post, put, delete.
  • Mount routers onto your main Express.js app using app.use().
  • Create nested routers for deeper organization.

Discussion