23 Nest JS How to use middleware in Nest JS and it's concept

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ต.ค. 2024
  • #nodejs #nestjs #javascript
    Middlewares are a very useful tool to control the behavior of your request lifecycle. Almost every framework will allow you to control the request using Middlewares and NestJs is no exception.
    Code: my-lnk.com/218...
    You can find me on:
    Twitter: / amitavroy7​
    Discord: / discord

ความคิดเห็น • 24

  • @nanditanikam1202
    @nanditanikam1202 ปีที่แล้ว +1

    Excellent explanation

  • @jagajaga6908
    @jagajaga6908 6 หลายเดือนก่อน +1

    Bro thank you very much, very useful tutorial!!!

    • @amitavroydev
      @amitavroydev  6 หลายเดือนก่อน

      You're welcome!

  • @devinicin
    @devinicin 2 หลายเดือนก่อน

    very good content bro!

    • @amitavroydev
      @amitavroydev  2 หลายเดือนก่อน +1

      Glad you liked it

  • @GabrielLogan17
    @GabrielLogan17 5 หลายเดือนก่อน

    In the case of the token example, wouldn't it be best to use guard ?

    • @amitavroydev
      @amitavroydev  5 หลายเดือนก่อน

      Yes that also can be used.

  • @zeroes5237
    @zeroes5237 2 ปีที่แล้ว +1

    thank you very very much brah!!!

  • @kelvinmandlik7395
    @kelvinmandlik7395 ปีที่แล้ว +1

    How can I access params in middleware it is coming undefiend. Header, body and query is accessible but not the path params (ex. /v1/user/:userId), userId is not accessible in middleware.

    • @amitavroydev
      @amitavroydev  ปีที่แล้ว

      You will get them inside the request object

  • @syedazeemjaved
    @syedazeemjaved 6 หลายเดือนก่อน

    Why not use a guard for it? What's the difference between middleware and guards in context of NestJS? Also to check JWT token, generally guards are used using PassportJS.

    • @amitavroydev
      @amitavroydev  6 หลายเดือนก่อน

      Your question is a genuine concern that many people including me had.
      The framework docs also address this and i quote
      middleware, by its nature, is dumb. It doesn't know which handler will be executed after calling the next() function. On the other hand, Guards have access to the ExecutionContext instance, and thus know exactly what's going to be executed next

    • @syedazeemjaved
      @syedazeemjaved 6 หลายเดือนก่อน

      @@amitavroydev Thankyou for the comment. So that means, a guard would have been better for this scenario, or when we need access to the execution context? I'll definitely be reading about it more.

  • @nikhilgoyal007
    @nikhilgoyal007 8 หลายเดือนก่อน

    thanks!

  • @jiteshjoshisde3154
    @jiteshjoshisde3154 ปีที่แล้ว

    Hi sir,
    I want to apply middleware to specific routes only, to check the user role by using user id from the headers token
    i want to apply the middleware to some routes only to check the user role . how can do that ??
    you registered to all the routes, how can we do ti for specific routes ??

    • @amitavroydev
      @amitavroydev  ปีที่แล้ว

      Look at the second code example on this page: docs.nestjs.com/middleware
      This should solve your problem.

  • @idriskodaolu4826
    @idriskodaolu4826 10 หลายเดือนก่อน

    please i need a full video that teaches nestjs

    • @amitavroydev
      @amitavroydev  9 หลายเดือนก่อน

      What I have is a series of videos

  • @vibhupandey5918
    @vibhupandey5918 ปีที่แล้ว

    excellent, I don't know it has only a few likes!!

  • @AmarjeetKumar-xr2gf
    @AmarjeetKumar-xr2gf ปีที่แล้ว

    How to get the user logged in value after token is validated in service module..for example if user is calling getAllRoles and validatation is successfull but in Role Module service i want to get the user logged in so that i can update the db who is changing the stuff...one way i no to use in each controller method user params but i want something like storage or i dont know which is directly accessable in any service

    • @amitavroydev
      @amitavroydev  ปีที่แล้ว

      I have handled that thing in the JWT strategy. When the token is validated, we have the ability to enric the user object with whatever points you want to populate.
      github.com/amitavdevzone/nest-js-quiz-manager/blob/master/server/src/modules/auth/jwt.strategy.ts
      Check this file for reference. Should help