Overview
This page contains 8 code snippets for implementing Routing in NestJS. Each snippet is production-ready and can be copy-pasted directly into your project.
Available Snippets
Roles Decorator
Role-based access control decorator
- Role enum included
- Single role requirement
- Multiple roles (OR logic)
Public Route
Mark routes as public to skip auth
- Skip auth guards
- Works with global guards
- Simple metadata decorator
JWT Auth Guard
JWT authentication with Passport
- Passport integration
- Public route support
- Token expiration handling
API Key Guard
API key authentication for services
- Timing-safe comparison
- Multiple extraction methods
- Multiple API keys support
Roles Guard
Role-based access control
- OR logic (any role)
- AND logic (all roles)
- Custom error messages
Throttle Guard
Rate limiting guard
- Customizable limits
- @Throttle decorator
- @SkipThrottle decorator
Timeout Interceptor
Request timeout handling
- Configurable timeout
- @Timeout decorator
- Custom error message
Zod Validation Pipe
Validation using Zod schemas
- @ZodValidate decorator
- Detailed Zod errors
- Per-route schemas
Frequently Asked Questions
How do I implement Routing in NestJS?
Browse the snippets above and copy the code that matches your needs. Each snippet includes installation instructions and usage examples.
Are these snippets production-ready?
Yes, all snippets are designed for production use. However, always review and test them in your specific context before deployment.
Can I use these snippets with other NestJS versions?
These snippets are designed for the current major version of the framework. Check the snippet documentation for version compatibility notes.