RESTful Web Services: Updating Data

Welcome to Part 4 of this review of the Pluralsight course RESTful Web Services with Node.js and Express by Jonathan Mills.


Jonathan is a JavaScript and Node.js expert working mostly in the MEAN Stack with individuals and companies to help build their technical skills to cope with the constantly changing landscape of software development.

He is also an ASP.NET insider and an international speaker focusing on JavaScript both in the browser and on the server.

Node.js and Express gives us the ability to write lightweight, fast, scalable APIs quickly.

Also in this series:

Part 1 – What is REST?
Part 2 – Getting Data
Part 3 – Posting Data
Part 4 – Updating Data
Part 5 – Testing
Part 6 – HATEOAS

Updating Data

Implementing Put

In this demo we write a put method using Book.findById and setting all the book properties to those from our request if there is no error.

Testing Put

In Postman we make a Put request and see that it is working.

the “__v”key comes from MongoDB and represents the version.

Now what if we only want to update a single property in the model, e.g. update the read value from false to true?

Instead of sending the entire contents of the book, we can make a PATCH request.


In bookRoutes we write a .patch method. Again this uses findById and instead of repeating ourselves over and over we use some middleware.

Middleware intercepts and processes a request, and then forwards that request onto the route.

We use middleware with app.use, and we are writing our own middleware here.

It does a findById and returns HTTP 500 response if there’s an error, or our book data as JSON if a book is found.

With this in replace we have implemented the Don’t Repeat Yourself principle.

Implementing Patch

We add a .patch method using a for loop over each piece of data in req.body

Testing Patch

We test our patch method in Postman and see that it all works.

Implementing Remove

The final verb to add in bookRoutes.js.

We add a .delete method and this returns a 500 if there’s an error, or a 204 if successful.

Again we test this in Postman to show that this code is working correctly.

Continue to Part 5 – Testing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s