ExpressJS — Improving upon our last work

On the previous story (click here) I explained how to make a simple RESTful api that could easily be tested with any browser. But an api can also take advantage of different header methods. While we have been using GET to pass and receive data, there are 4 methods available to express.
- GET
- PUT
- POST
- Delete
There are actually a few more but they aren’t as important now.
But a browser can’t just send a request with this methods without any tinkering, so we will use a free tool to get access to those methods.

Advanced Rest Client is the tool we are going to use, because its simple, easy to understand and open source.

Assuming you have already seen the previous story you already know the basic code, to adapt it to this methods is simple. We just need to use this words were we were using “get”. As you can see bellow.
Now with ARC we will send different methods and we are expecting the same answers we got when using get and the browser to test.
GET:

Starting with the GET method we test how it shows results in a way we already know should work. As it has worked before on the browser.
PUT:
We will use the PUT method to insert a new entry to the users array. This new item will have the atributes username and email that we sent on the URL.
In this example they will be test and something@test.pt respectivly.

And as we can see the value is there, if we were to request the list again the same result would appear.
Delete:
Lets say we made a mistake when adding the user. Using this 4 methods the obvious choice is to delete it and try adding it again this time without errors.

Post:
Post can be used as a alternative to put. There are some differences about reliability and security that are out of the scope of this story. Just as a extra fact this is the method html forms usually rely on to send information back to the server.
Because you can send back a response to a POST request it can be tested in the same manner a GET would.

We get back the information of the user with id = 1 in the response, meaning the app.post() function worked as intended.