**Can you guess the output of this JavaScript code?**
In this Node.js REST API tutorial, we'll understand the **PATCH method** through step-by-step animations and learn how one important JavaScript line updates an existing object:
What happens when we combine an existing course object with new fields? Which properties remain unchanged, and which values get overwritten?
We'll understand all of this visually while building a practical REST API using Node.js.
**What you'll learn:**
- What is the PATCH method in REST API? - How PATCH requests update existing resources - JavaScript spread operator (`...`) explained - How to merge two JavaScript objects - How object properties are overwritten - Why the order of spread operators matters - How to update selected course fields - How to handle empty PATCH requests - How to protect fields using an allowlist - Understanding JavaScript object spread through animations
This lesson is useful for JavaScript beginners, Node.js backend developers, and students preparing for JavaScript coding interviews.
Learn backend development with practical examples and easy-to-follow visual explanations.
This video provides a detailed technical explanation of how to implement a PATCH request in a REST API using Node.js and Express, focusing on the logic used to partially update data within a JSON array.
Hey guys, today in this video I'll explain to you animation through the full lens of
How can we perform patch requests on these our REST APIs
But before that, Guyce, I would like to request you, please edit the video right away
Pause because this particular function here is this particular one
The line is visible to you; you need to solve it and tell me what this updated version is for me.
So you can see, basically in the courses we are reading the data
are, where from, this is my json, so basically here total 12 you have array
There are 12 elements in total inside an array.
which basically means that every single element is an object for you,
So you've got this in the courses, okay, this index here,
basically, we now need to find out when the user submitted the patch request
When the user submitted the patch request
Here it is, meaning this request is for API slash
Course 7, the seventh end, and in it I did something extra
The field that has been added; basically, I need to update this.
Doing this, which is my particular field, to me
I need to update them, okay, what should I do? I need to update these people.
So I'm putting it here, so I hope now you've understood.
You have the ID, you've found out the courses,
The fields we are basically allowing, because we don't want this.
so that any irrelevant field added there by him becomes ours
Inside the element, inside the object, this is allowed among these
Whatever is appearing before you here
We will allocate the Fields bus whenever the matter arises.
The patch update will come, it's correct, so this is yours
Alaud has now told you everything.
Now tell me, what output am I going to get from this?
This updated variable of mine, basically the one that I have
New object: What will be available here? And what about this line?
What does this line mean? What does it mean that's written here?
What does it mean? Please tell me right away in the comment.
Let's say the one who spoke together here
I keep my companion here too.
I copy this into the service.
Alright, do it this way
I pay it here.
Paste all. Okay, first of all.
Who is on the seventh number, we'll take a look at that
This one is at the seventh number.
Part you can see here REST API design
What's going on, and the price is 3999 here, price 4
Let's see, if it happens, then it happens. If it happens, then it happens.
And boom, guys, check this out — this is yours now
REST API design is equal to
This is how the patch is working.
So how is this task being carried out now, and what does this line mean?
Let's go with what I promised you
We are fully into animation now; let's understand this step by step.
So guys, you can now see here the request
Now let's understand this completely, step by step, with full animation, as per your request.
You'll really enjoy it. Let me know how you feel about it. Alright then, this is the first one.
Your line, from which we receive the ID and data, and there's no need to speak the ID
This is your ID. This is your data. What the user wrote was correct, that's fine.
Now, what is this ID and data? Let me explain a simple flow to you.
So it's very basic, look, this is my app
We have used the router, simple is fine, Express Router.
So this is my prefix: API / courses, API
And the sleigh courses, you can see them, and this is the ID
It is and this is your body that we want to update
This is my ID, this is my body, it's fine, so I hope you
It's been figured out that here, check the request on the app
Here we have made the patch.
So you can see, the patch
Here is the ID
By default, we have set up the dynamic segment in this manner here.
The course will be updated; it will basically go to the controller now.
So, the job of the controller is
If there's anything in the request, get it.
Then pass whatever data there is to the service.
What will be obtained there as a response
Forward it
So this is my request.params.id
What did I get? ID got it from this
I've got this ID already
So what have we done now? What we requested
And the body, obviously, what did I get from the body, this one
The user also came here to apply the patch basically for updating.
The fields you provided have already been given to me, right? So, give me the ID.
Got it, the field I got, converted into a number
Do nothing else, and we have updated the course that ours
Service has been passed there; the rest is response.
We will forward it further, but this is our service now.
So we have a course, sorry, no, we have an ID
Okay, whatever we got, 7 and the data that the user wrote, just here.
You'll find out what the ID is, and then whether your data is there, simple.
Alright, alright, so we move to the next line, okay? Where we have the course
If you're reading it, then the total we have is 12, we've done it this way.
When you read it, inside the courses, these 12 out of 12 are mine
We will have to find out who wants to be updated.
Yes, then only your find index and through in
Look, we found out the ID is 7, okay, so the ID is 7, but the index should be 6, right?
Brother, please keep these points in mind—they are very important.
So, the data corresponding to index number 6, look at this, it is this,
basically in courses[6] we got, and this fields,
Do you remember I told you which fields are allowed?
We will allow updates for it, but not for the rest.
As we can't provide ID, we entertain the admin
Now we need to check this object.keys field
Look here, carefully understand what is happening right here.
This happened by using object.keys and fields
That I may obtain these two things
Price and then is life
basically, the object I received
The data that the user had sent
When we had it through allowed fields
These two came to me after filtering.
Price and then is life
So if we find out its length right now
So I know that right now we have
Total two elements
There are two elements here with us
So two is not equal to zero.
I mean, don't skip this.
Because, you know, what happens if someone just sends this empty object?
That's why we're handling this part.
Now, the most important part, we have reached there.
First of all, empty curly braces — we need to create a new object,
Alright, now guys, this is the most important part right here,
where inside the courses we have placed the index number in square brackets,
So our index number is 6, okay, so where did this come from, tell me?
Here it is, these are our courses, right here
Here, I received the entire data.
And I'm spreading it out for him.
Alright, that's exactly what's happening, look here.
So we can see that we have here
The entire data has been obtained in this manner.
So, look inside my updated version — the original hasn't been touched.
We've made a copy of it and are spreading it around.
We made a copy of the original.
And these are my fields, which contain only two.
Look, we've already changed the price.
Now this isLife which was false before
Now we'll do it to him too, true
Of all those, the one whose copy we took
In it, there were these two particular fields
We've already updated it for him.
That's what a patch means, that's what an update means.
The old course is exactly as it is.
But actually, we've already updated it here.
Now, carefully look here — just one method is described, namely, old first.
new second, otherwise the old data will override your update,
If you first spread the fields in this manner and then this one
If you were to spread the courses, then your guy would be the one who...
The original value was that, then again, it doesn't work at all anymore.
Alright, I just wanted to show it to you, guys.
The rest that remains now, you figure it out yourself.
Tell me below in the comment section
What does it mean? It's simple, this is what we tried, whatever mine
Got the updated value, sent it to him in our courses.
Replace it with that index number.
And he wrote it down; now go to that place
Finally, now our actual API is that one
It will be changed, okay. The rest will return updated.
Simple, that's all, so if you
Do you remember, in yesterday's video I also mentioned Create API
It was made above, okay, so today as well
I hope you have learned something new.
So, guys, how did you like this video and what do you want?
There are some important things, small and large
When we create some functions
Some lines come up that are important.
Yeah, and I think you might feel like, dude, in that one
It's a bit tricky, but if I explain it to you, it'll make sense.
Let me know in their comment section, okay? You just write this, I'm
I will understand what you want me to do something important
One-liners that are extremely useful
I'll keep making videos about him; the rest will come in single shots.
You'll enjoy many languages, that's correct.
But yes, guys, that's it for today's video
If you like the video, please like it and share it with your friends.
And the next target is this 8K like subscriber
Please subscribe now if you haven't already
See you in the next video, guys. Thank you so much. Take care.