Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Intelligence View
⚡ tsecurity.de Intelligence

Nest.js Nested Routing, Wildcard, Request

In previous blog, we understood about Nest.js controller and now we will be exploring a bit about nested routing, wildcard and starting with nest.js request. Nest.js Nested Routing: A Modular Approach Nest.js encourages a…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In previous blog, we understood about Nest.js controller and now we will be exploring a bit about nested routing, wildcard and starting with nest.js request.






Nest.js Nested Routing: A Modular Approach



Nest.js encourages a modular and organized structure for building applications. One key aspect is nested routing, where routes are organized hierarchically, mirroring the structure of your application. This approach provides clarity and maintainability, making it easier to manage the complexity of larger applications.



This is a simple Nest.js controller:




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

@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats of Shameel!';
}
}






If you send GET request to /cats/ then it will return 'This action returns all cats of Shameel!'.



In order to make nested routes within this controller for GET request handler for this case, all you have to make changes is in @Get() decorator to something like this:




@Get('/shameel/123')






Now you can access this route with this:




/cats/shameel/123









Wildcard Routes



Wildcard routes in Nest.js provide a way to capture dynamic or unpredictable segments in a URL, enabling the handling of various scenarios within a single route.



You can make changes like this:




@Get('/sha*eel/')






Now you can access it with any character between "sha" and "meel". For example:




/cats/shameel
/cats/sha1eel
/cats/shaweel









Nest.js Request



In Nest.js, the @Req() decorator is used to access the underlying Express.js request object. This decorator allows developers to tap into the details of an incoming HTTP request, gaining access to headers, parameters, and other request-specific information.



For better typing in TypeScript, please install this as dev dependency.




npm install @types/express --save-dev






Now you need to replace the controller code with this:




import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

@Controller('cats')
export class CatsController {
@Get()
findAll(@Req() req: Request): string {
console.log(req);
return 'This action returns all cats of Shameel!';
}
}







As you can see that we have added @Req as a decorator and when you check your terminal after hitting /cats/, then you will observe request object similar to express.js.



Follow me for more such content:

YouTube: https://www.youtube.com/@ShameelUddin123

LinkedIn: https://www.linkedin.com/in/shameeluddin/

Github: https://github.com/Shameel123

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Nest.js Nested Routing, Wildcard, Request

Thematisch verwandte Begriffe: Nestjs, Nested, Routing, Wildcard · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY Kritische Sicherheitsmeldung
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick