From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Defining custom metadata

Defining custom metadata

Next, provides an ability to attach our own custom set of data that is metadata to any route handler methods or classes. And that metadata can then be accessed by guards, interceptors or other decorators to alter the behavior or make any decision in the application. So let's see how we can set custom metadata. I am continuing with the previous example. I opened the app controller file and here I'll attach a custom metadata to get route handler by using the set metadata decorator. This decorator allows us to add custom metadata to a route handler. It takes two arguments, a key value of type string and a pair value. So here let me set the key as roles and the value as admin. This is how the key and value that is the metadata is attached to a route handler. And we typically follow the syntax for giving the value like this only that is a square bracket and a string value. If I hover on the decorator, there you can see the syntax in the description as well. So it's an array of values. Now…

Contents