ExpressJS — Improving upon our last work

Cris Ferreira Pires
3 min readMar 20, 2021
Express JS

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 Twitter Banner

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

Advance Rest Client UI

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:

Get Result

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.

PUT Result

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.

Delete Result

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.

Post response

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response