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.

Applying role for specific handlers

Applying role for specific handlers

In many applications, especially those with diverse user bases, it's crucial to control access over certain features, or route handlers based on user roles. And that's what we will be seeing in this example. That is how we can set certain route handlers for specific users only. So let's begin. First inside the user service file, I will create one more method that is addUser method. To add a new user, I will give the user of type any in the argument and return this.usersArray.push and user. Now inside the controller, let's define the post route handler. I'll give the post handler and the route path as create. We'll give a method called create, which takes the body decorator, the user data property of type any to store the request body. Then to add the user, I'll give this dot user service dot add user, user data, and we'll return a message user added successfully. Now the next step is somewhat related to the database. Right now we don't have any database connected. So we need to create…

Contents