When we want to allow our users using our API to insert or update data, we need to accept a request from a different HTTP verb. When inserting new data, the POST
verb is the preferred method to accept data and know it's for an insert. Let's take a look at code that accepts a POST
request and data along with the request, and inserts a record into our collection and returns the updated JSON. Insert the following block of code after the route you added previously for GET
:
router.post('/', function(req, res) { // insert the new item into the collection (validate first) if(req.body.Id && req.body.Title && req.body.Director && req.body.Year && req.body.Rating) { json.push(req.body); res.json(json); } else { res.json(500, { error: 'There was an error!' }); } });
You can see the first thing we do in the POST
function is check to make sure the required fields were submitted along with the actual request. Assuming our data checks out and all the required fields are accounted for (in our case every field), we insert the entire req.body
object into the array as is using the array's push
function. If any of the required fields aren't submitted with the request, we return a 500 error message instead. Let's submit a POST
request this time to the same endpoint using the Postman REST Client. (Don't forget to make sure your API server is running with node server.js
.):
First, we submitted a POST
request with no data, so you can clearly see the 500 error response that was returned.
Next, we provided the actual data using the x-www-form-urlencoded
option in Postman and provided each of the name/value pairs with some new custom data. You can see from the results that the STATUS was 200, which is a success and the updated JSON data was returned as a result. Reloading the main GET
endpoint in a browser yields our original movies collection with the new one added.
PUT
requests will work in almost exactly the same way except traditionally, the Id
property of the data is handled a little differently. In our example, we are going to require the Id
attribute as a part of the URL and not accept it as a parameter in the data that's submitted (since it's usually not common for an update function to change the actual Id
of the object it's updating). Insert the following code for the PUT
route after the existing POST route you added earlier:
router.put('/:id', function(req, res) { // update the item in the collection if(req.params.id && req.body.Title && req.body.Director && req.body.Year && req.body.Rating) { _.each(json, function(elem, index) { // find and update: if (elem.Id === req.params.id) { elem.Title = req.body.Title; elem.Director = req.body.Director; elem.Year = req.body.Year; elem.Rating = req.body.Rating; } }); res.json(json); } else { res.json(500, { error: 'There was an error!' }); } });
This code again validates that the required fields are included with the data that was submitted along with the request. Then, it performs an _.each
loop (using the underscore module) to look through the collection of movies and find the one whose Id
parameter matches that of the Id
included in the URL parameter. Assuming there's a match, the individual fields for that matched object are updated with the new values that were sent with the request. Once the loop is complete, the updated JSON data is sent back as the response. Similarly, in the POST
request, if any of the required fields are missing, a simple 500 error message is returned. The following screenshot demonstrates a successful PUT
request updating an existing record.
The response from Postman after including the value 1
in the URL as the Id
parameter, which provides the individual fields to update as x-www-form-urlencoded
values, and finally sending as PUT
shows that the original item in our movies collection is now the original Alien (not Aliens, its sequel as we originally had).
18.226.104.202