Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Working with Route Parameters | Controllers and Routing
Building Backend Applications with Nest.js

bookWorking with Route Parameters

Swipe um das Menü anzuzeigen

Route parameters allow you to pass dynamic values in the URL.

For example, instead of requesting all users, you can request a specific user by ID.

Define a route parameter like this:

import { Controller, Get, Param } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get(':id')
  getUserById(@Param('id') id: string) {
    return `User ID: ${id}`;
  }
}

  • :id: defines a dynamic part of the URL;
  • @Param('id'): extracts the value from the URL;
  • id: is passed into the method.

When you open:

/users/5

The response will be:

User ID: 5

You can use route parameters whenever you need to work with specific data, such as users, products, or orders.

question mark

What decorator is used to access route parameters in Nest.js?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 2. Kapitel 4
some-alt