Nest.js | Middleware, Pipes & Interceptors Explained By Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024

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

  • @mguay
    @mguay  7 หลายเดือนก่อน

    Get my highly-rated Udemy courses at a discount here: michaelguay.dev/udemy/

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

      Hey Michael, I followed your tutorial on nest js microservice using RabbitMQ, but I did not find if you used service registry. My idea is to have an auth microservice and an Api gateway app listing on 3200 that essentially work as a proxy forwarding request to auth. I looked online, most of them were too complexes. Can you help?

  • @williamx0
    @williamx0 2 ปีที่แล้ว +36

    Wow this was amazing! Thanks so much for teaching us all of this again! As always lots of knowledge gained from watching your videos. Many thanks! What I learned is that the path a request goes through in order is...
    1) Middleware - For things like authenticating a user
    2) Guards - To check a user is authenticated before getting to the controller (manager of the task) among other use cases
    3) Interceptor (before) - Can be used to log the method, url, userAgent, ip, controller name (ie., AppController), controller handler/method name (ie., getHello). Can also be used to transform incoming data
    4) Pipes - Can be used to freeze parameters in the request so it isn't mistakenly changed in the code. Also validate input data, transform input data, etc. Typical usage is validating request parameters with built in NestJS pipes like ParseIntPipe, ParseArrayPipe, ParseUUIDPipe
    5) Route handler - The "manager" that tells the provider (ie., service/worker/task implementer) what to do
    6) Interceptor (after) - Can get the response and status code after handled by the control handler (ie., the method/function inside the manager). Useful for logging. Can be accessed via next.handle().pipe(tap(res=>{}))
    7) Exception filters - To log errors internally or change the error response into user friendly wording for better UI
    Then it becomes a beautiful response
    Wooh! Just wrote the above to summarize what I learned - all info from going through this video. Thanks again and looking forward to your future videos! And ofc I gave your video a like as it's very helpful.

    • @mguay
      @mguay  2 ปีที่แล้ว +4

      Thank you! And that is a great textual summary.

  • @SHIVAMPANDEYSJTG
    @SHIVAMPANDEYSJTG 2 ปีที่แล้ว +12

    One of the best explanations I have seen. In just 30 minutes every things is explained in such a depth.

  • @thestereokid6073
    @thestereokid6073 วันที่ผ่านมา

    this is so great, been trying to figure out how I'm gonna get all the userIds to track operations and permissions. Gonna watch this properly next week and refactor my code.

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

    I've been using NestJs for 2 years and wirh this video I have witnessed the power of scoped dependency injection. Thank you!

  • @TheSiddhaartha
    @TheSiddhaartha 2 ปีที่แล้ว +4

    This is a useful example to quickly understand the concepts of NestJs. I had forgotten all of it as I didn't use it for few months. Now trying to quickly revise the concepts and also understand other things which I had not used before. Your videos have been very helpful!

  • @warpmonkey
    @warpmonkey 2 ปีที่แล้ว +3

    Great video, you deserve many more subscribers, keep up the great work.
    I love it that you take the time to keep going through the different ways that features can be hooked into the code base, it's that type of repetition that helps teach the pattern rather than summarise the options.

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

      Thank you very much!

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

    I just got some lessons on good architecture here :)
    The request service and freezing requests, am definitely applying that onto my current toy project and beyond.
    Thanks a lot Michael

  • @anouarzougrar8332
    @anouarzougrar8332 8 หลายเดือนก่อน +1

    my brain melt down when i saw that request provider scoop exists 🤯🤯🤯

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

    One of the best explanations I have seen. Thank you so much for the great video Michael.

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

    Awesome Explanations and fluent demonstrations on Request-Response Pipeline for Nest JS . I really appreciate your efforts and thank you very much for such deep insights !!

  • @johnmark-ps8jy
    @johnmark-ps8jy 2 ปีที่แล้ว

    Concept simply explained. You deserve more subscriptions. Thank you very much!

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

    This is a great video, I wanna say thank you for explaining these concepts in an easy way

  • @kisstamas6675
    @kisstamas6675 2 ปีที่แล้ว

    This video is very useful. The nestjs framework is huge. It is similar the complexity like Laravel in php language.

  • @marudhuraj6709
    @marudhuraj6709 2 ปีที่แล้ว

    Super explanation about the framework flow architecture, Really a great job.

  • @user-ym9fp2je8u
    @user-ym9fp2je8u 2 ปีที่แล้ว

    All of this stuf were explained simple and clear... Thank you!

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

    Thank you!! what a nice and clean way to explain how nest works

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

    Nice walk through

  • @atakandurkal2400
    @atakandurkal2400 3 หลายเดือนก่อน

    Very well! explained clearly now I can move on :))

  • @supermanor
    @supermanor 2 ปีที่แล้ว

    thanks so much for this video it helped me alot understanding the response request path and how to implement it correctly

  • @priyankdev.
    @priyankdev. 3 หลายเดือนก่อน

    thank you this sharing amezing video and keep it to help my codding careet

  • @idcmardelplata
    @idcmardelplata 7 หลายเดือนก่อน

    As we say in my country, you left 10 fields... Congratulations, your content is too good :)

    • @nodemodules
      @nodemodules 9 ชั่วโมงที่ผ่านมา

      Y'all ok over there?

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

    Can you also do tutorial on nestjs prisma using pg db to do cascading delete, soft delete, and many more logical concept or ideas?
    Can you also update or making new series on socket io backend server using nestjs prisma orm as use of pg db?
    Thank you for this tutorial🙏🏽

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

      A Prisma video is in the works.

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

    Greate tutoria please create more video about the full authentication process and authorization with JWT, thank

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

    Thanks for the great video.
    But I have one clarification: we can still use global interceptors in main.ts
    Example:
    app.useGlobalInterceptors(new LoggingInterceptor(await app.resolve(RequestService)))

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

    Thank you so match,All love.❤

  • @saeedakhshijan8159
    @saeedakhshijan8159 2 ปีที่แล้ว

    wow, OMG, sec by sec was useful for me. thank you.

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

    Great video man! a quick question, slight off topic. Could you please explain the bit where @Catch() is used. Have seen a lot of examples where they always catch http expection. Are there other other types of exception ? Wat all types are allowed ?

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

    hi, this is a beautiful tutorial. Thank you!!!
    But i have a question, why in the interceptor, the next.handle() return an observable? i am expect something like:
    const result = next.handle();
    // do something with result
    return result;
    Thank you!

  • @10x-dev
    @10x-dev 2 ปีที่แล้ว

    Awesome please keep posting videos like these!

  • @mohammadalathamena
    @mohammadalathamena 2 ปีที่แล้ว

    this is awesome , i hope you will post video like this in future

  • @xavierhaas2560
    @xavierhaas2560 2 ปีที่แล้ว

    Very nice tuto. Good exemples, it helps !

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

    Thanks a lot for the video!

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

    This is amazing! Thanks Michael

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

    Awesome Awesome Awesome Awesome Awesome

  • @kirakira160
    @kirakira160 2 ปีที่แล้ว

    Thank you Michael !!! You are The best🤩🤩

  • @fazliddinalimov6993
    @fazliddinalimov6993 10 วันที่ผ่านมา

    for some reason the method getUserId of request service returns undefined in other controllers than app

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

    A.M.A.Z.I.N.G (truly)!

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

    Thanks for the explanation. It's quite clear to understand the differences between Middleware, Pipes, and Interceptors 👏.
    There is a catch though. You are injecting "scope Request" like Santa is giving candies 😃.
    But this has huge side effects. For example, in the user service, it means that the app will have a new instance of the service for each request and moreover, a new instance of every class this service is injected into. This will massively slow down the app. And same for the LoggingInterceptor: each request = a new instance of the logging class → memory-intensive.
    How can we overcome that? What would you recommend as an alternative to your current solution?

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

      Hi thanks - and great question as this is a valid concern.
      I recommend using Node AsyncLocalStorage. You can find other packages wrapping it like this one: www.npmjs.com/package/nestjs-request-context

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

    You are amazing man.

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

    Thanks a lot!
    Everything worked fine !

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

    Great work thanks

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

    Could you tell me where would it be best to place business logic in nest app? The example of such business logic is checking if resource that is about to be created has a unique name, however the name uniqueness is scoped to some information inside request object (i use passportjs which ends up attaching decoded jwt content in the request object), or e.g. checking if referenced resource exists when updating - then id of the resource is one of the URL params and update data is the body? I cannot find anything in the docs that will fit my needs. Please help

  • @demoncodewithme3974
    @demoncodewithme3974 9 วันที่ผ่านมา

    thanks man I really need this ..

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

    Nice explanation but `Guards` are supposed to be used for Authentication and Authorization as long as I know in Nestjs app

  • @barkaouimohamed
    @barkaouimohamed 2 ปีที่แล้ว

    Thank you for the video !

  • @shwackthenoobsac
    @shwackthenoobsac 2 ปีที่แล้ว

    Wow awesome video. Thank you.

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

    Can u explain please why we need to set scope as request? Cuz thats not so clear in this case

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

    AWESOME!!!

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

    Sir how can stored the logging information with http response in file?

  • @GriffinKlett-ig6vt
    @GriffinKlett-ig6vt ปีที่แล้ว

    Hi @Michael, Does this order change at all if an exception is thrown by something before the route handler?
    Ie, I was getting a 413: RequestPayloadTooLarge, which I think is an error from Express, and my global ExceptionFilter was being triggered but not my interceptor.
    If the error occurs anywhere inside the actual method, then the order happens as I'd expect from this video.
    Thanks

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

    Thank you ❤

  • @SaidkamolO
    @SaidkamolO 2 ปีที่แล้ว

    Can you make videos on configuration, caching and task scheduling on nest js? Also websokets are appretiated.

  • @vladimiragaev4360
    @vladimiragaev4360 2 ปีที่แล้ว

    Very good explanation. I couldn't have done better :)

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

    How do you inject the request service in other modules? Does not work for me.

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

      Same here! It only works in App Module

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

    Very nice! Thx! 👍

  • @sylvain7010
    @sylvain7010 2 ปีที่แล้ว

    does a middleware or an interceptor can be used to check if incoming request has some value in header, like accept-version, or token etc (without checking the content of the value) ?

    • @mguay
      @mguay  2 ปีที่แล้ว

      Yes! This is a great place to make that check.

  • @TheWalrus_45
    @TheWalrus_45 2 ปีที่แล้ว

    Hi Michael - thanks for the great video!
    Have you tried out v10 of nestjs graphql? Its plug-in based now and supports Mercurius and as of v10.0.6 is supports federated subscriptions which would be cool to see.

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

      Thank you! I played around with it briefly, but I will have to take a closer look into federated subs.

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

    just putting it out there, but was anyone else ready to smash their keyboard at the wildcard route not working for the middleware? i tried every which way bloody regex combination and in the end, an empty string worked. i hate my life. Nest v10.1.0 in an nrwl/nx monorepo for reference....

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

    the best

  • @mrs013
    @mrs013 2 ปีที่แล้ว

    I dont get it :'(
    How long you learn nestjs until you can understand that diagram?

    • @mguay
      @mguay  2 ปีที่แล้ว

      Try looking up some example use cases online and then try to implement for yourself in a side project.

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

    my question is simple : WHERE DO U GET ALL THIS KNOWLEDGE LOLLL (btw thank you soo much for the video !!)

  • @jafar1607
    @jafar1607 2 ปีที่แล้ว

    Brother. A quick help pls
    Nestjs and mongoose. Nx monorepo
    I have organisation module, product module and category module.
    - product schema defined in product module.
    - category schema is defined in category module.
    - organisation schema defined in organisation module. But schema has a array of products and category as subcollection to org collection.
    - when I create a product or category, I need to use organisation model in product and category module. So i would need to import organisation module in product and category module
    - since I have product and category as subcollection to organisation schema. I need both product and category imported in organisation too.
    This makes it circular dependent. How to approach this use case. Plz help.

    • @TheWalrus_45
      @TheWalrus_45 2 ปีที่แล้ว

      Prisma is gonna be the end of Mongoose.

  • @harrylinuz6731
    @harrylinuz6731 2 ปีที่แล้ว

    thank you! and subbed!

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

    this guy is so pretty