MVC Model-View-Controller Example | CRUD REST API | Node.js & Express tutorials for Beginners

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

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

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  3 ปีที่แล้ว +7

    MVC (Model-View-Controller) is a popular design pattern for organizing your code. In this tutorial, we organize our Express REST API with MVC. In addition, we complete the CRUD operations for our simple JSON data. If you are just starting out with Node.js & Express, you may want to start at the beginning of this Node.js for Beginners playlist found here: th-cam.com/play/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw.html

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

    Я лайкаю все новые видео сразу же. Ибо результат всегда на высоте гарантированно! А потом уже смотрю, когда есть время. Большое спасибо!

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

      Thank you! I needed to use Google translate to understand, but I appreciate your message! 🙏💯

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

      @@DaveGrayTeachesCode Sorry, Dave, my english is ugly a little bit, and I wrote my comment in russian (my native language). And also I want, let you know, you have fans in Russia! I often send links to your channel to my IT friends.

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

      @@cradleofkaschenko2057 thank you so much! You can continue to comment in Russian. I think Google does a good job translating. 💯🚀

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

    Thank you so much Dave :). This course has really made my transition into backend easy and interesting 🤝

  • @ahmad-murery
    @ahmad-murery 3 ปีที่แล้ว +6

    Beside organizing the code, what I really like about MVC design pattern (or alike) is that we can separate the code facing the outside world (controller) from other codes dealing directly with the data (model),
    regarding createEmployee handler, getting a new id by incrementing the last employee id will work fine as long as our data is sorted by id (which is the case here), but if we for some reasons have the data unsorted or sorted by something other than id then I think it cold be better to find the max id + 1
    anyways, using a real database engine IDs can be auto generated,
    for the unsortedArray.sort function, although it's much clearer this way but it can be made shorter by returning a.id - b.id (I think you mentioned this in a previous video but don't remember where)
    Thanks Dave for keeping us thinking

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

      Thanks Ahmad! Great notes here my friend. 🙏💯

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

    Спасибо!!! Отличный канал, прекрасный педагог! Успехов!

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

    you're an amazing teacher!! thanks for sharing this tutorial and keep doing them!!!

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

    Thank you very much.
    In this lesson (after copy-pasting your source code) -
    Thunder Client doesn't respond to requests
    While the server runs on port 3500.
    It did work during the Router video (where I manually wrote anything you did).

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

      There must be some difference as I do not have that issue here. I might suggest using git clone instead of copy & paste. You can download my source code repositories with git that way.

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

      It is beautiful to see it's working..

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

    interested way to explain everything was amazing to understand ...well explantion thank u

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

    Can we just use sort((a, b) => a.id - b.id) instead of sort((a, b) => a.id > b.id ? 1: a.id < b.id ? -1 : 0) in updateEmployee when sorting the array?

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

    Thanks Dave, great work !

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

    Hi Dave, cool node+express series, as well as a channel in general, I love the way you explain details. Can you advise if there is some balance between the amount of code in Routers and Controllers? I'll explain a bit. I had 90% of all my code directly in the Routers, inside the "(req, res) => {}" functions, I thought the code would work faster, neglecting the convenience of writing, refactoring, etc. I had literally thousands of lines of code in my routers.
    Recently I've redone everything and transferred all my code to the respective controllers. However, I believe, there should be some balance between the two. A lot of similar imports in my controllers make me think about some sort of inefficiency. I came from very ancient languages like Assembler, Basic, Pascal, etc. :)) where repeating was treated as something very bad and inefficient, even an extra semicolon was bad)) not to mention entire models or other controllers. The logic behind was really simple - the compiler will bring all the imports into 1 file anyway. I realize that the browser does not compile JS))) however, the entire chain ends up on hardware anyway... Thus I'm wondering about your approach, experience, or even thoughts in this regard, thank you in advance.

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

    Excellent reference as always!! Thank You Dave….

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

    Great tutorial! I learned something new. I really appreciate the explanations

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

    I took a small brake because the Course became very advanced. It is hard to progress building the project at the same time when it is at the beginning level. Now I am practicing very basic "Express JS Crash Course" by Traversy Media youtube channel, but I am coming back soon, and surely I will finish it.

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

      Thank you Piotrek. I understand and fully support learning at a pace you are comfortable with. 💯

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

    Hi, I'm wondering whether I should be using immutability for my functions or not(I've done mine without and tested it out and it seems to be working fine), additionally using immutability consumes more data storage from what I know, and for a larger amount of data that could come into play, afterall im updating the existing database always and not creating a new one so it shouldnt be required but maybe its better viewed or actually is better in some other way or maybe I'm supposed to use it interchangebly depending on each case? Thank you for your great videos

  • @Stoic.Outdoor
    @Stoic.Outdoor 3 ปีที่แล้ว

    Thanks for your hard work bro.

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

    I have a question on part 14:20. When you run employee.firstname = req.body.firstname, wouldnt it mutate the existing employee object in the existing array? In that case isnt it unneccesary to remove the object from array and add it again?

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

      Good thoughtful question! You are receiving an employee object from the find() method that was called. It is stored in a new variable named employee. We are updating the object held in the employee variable. It is not in an array. It does not mutate the pre-existing object in the database. We then filter the employees array (not to be confused with the employee variable) to remove the old employee object and replace it with a new one.

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

      ​@@DaveGrayTeachesCode I am sorry for asking again. But what I meant was that find() method will give an employee object, but it is the same object in the data.employees array. The variable name employee is just a pointer to that existing object in the array. Yes, as you said it doesn't mutate the pre-existing object in the database, but it should mutate the object in data.employees array.

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

      ​@@skyhs0168 right ! true that
      coz employee variable will be referencing the object from the array and thus any changes made to it will appear in the array as well

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

      @@baxtables yes it does mutate the object in the array.

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

    6:49 how we handle put and delete to this path without dinamic paramethar like :id?

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

    I liked very much
    Keep up

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

    Great tutorial on getting started with Express using the MVC Pattern. Thanks, Dave
    {2022-06-10}

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

    So good tutorial thank you

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

    Thanks!

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

    Hi, Dave! First of all - thank you for all the great tutorials!
    I have a question about response data transformation. Should it be done in the controller layer or should there be an additional "service" layer between the controller and the model?
    Thanks!

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

      You're welcome! 💯 Either could work. The data is often transformed by middleware which is essentially what a service layer would be before it reaches the controller.

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

      @@DaveGrayTeachesCode Thank you for the prompt reply!

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

    Hi Dav, Grate tutorial, Thank you for that. I have a question we have different libraries for API development expressJS, NestJS many more can you suggest me which is best for a realtime steaming application which should read data from a data bricks server continuously

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

      You mention some specific requirements here that I don't feel I have enough experience with to give an opinion on which is *best* . You can usually achieve your goals with multiple solutions so I would define _best_ as the one you are most familiar with.

  • @cihat-kocak
    @cihat-kocak ปีที่แล้ว

    cool

  • @13thdisciples28
    @13thdisciples28 3 ปีที่แล้ว

    Could you please upload a video that explains authentication; like how to consume a signup and a login endpoint with Javascript or react? thanks

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

      You are almost reading my mind! Authentication is the next thing I wanted to add to this Node & Express series before moving to database options. I'll focus on the backend, but a separate tutorial could do the frontend in detail, too. 💯

    • @13thdisciples28
      @13thdisciples28 3 ปีที่แล้ว

      @@DaveGrayTeachesCode Thank you so much!!!! I will be on the lookout! It's my third year as a programmer I haven't been able to scale that part of my learning curve. and I have not had access to quality tutorials out there

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

    🚀

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

    One thing appears to me looking your great video. Having separated 'routes' and 'controllers' layers you should avoid use of http related APIs inside the controller. It should not be coupled to any platform and contain only business logic. Controllers should be easily portable to desktop GUI app and should have nothing to web/http staff. All data passing from web layer and http interaction should be handled by 'routes', and your routes are pretty "dumb" passing all the logic (even web handling one) to controller.

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

    why u make video ?????

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

    For sorting the array in ascending order with respect to employees' id in the updateEmployee() function, it would be better the usage below. Just as a recommendation.
    data.setEmployees(unsortedArray.sort((a, b) => a.id - b.id))