From the course: Building a Website with Node.js and Express.js

A little intro to REST APIs

From the course: Building a Website with Node.js and Express.js

Start my 1-month free trial

A little intro to REST APIs

- [Instructor] REST stands for Representational State Transfer, and it defines a way for creating web services. Today, such RESTful services are the most commonly used pattern for APIs. You find courses dedicated to REST with Node.js in the library, yet in times of single-page apps, it's such a common use case for Express that I want to cover it in this course as well. In this chapter, we will use some client-side JavaScript and the REST API to send feedback directly to a REST endpoint and also to update the webpage without reloading it, but let's first look at the basics. When we talked about routes, we already touched the topic of HTTP verbs. Get is used when you simply request a page, post is most commonly used for forums because it allows to send large amounts of data in the payload, put is nothing the browser uses, but it's part of the HTTP standard, and delete is also defined in the HTTP standard and not really used by browsers, and there are a few more verbs, but these are the most common used ones in REST services. Now, what REST does, it's giving those HTTP verbs a meaning, or how we call it a semantic. Get requests a resource, post creates a resource, put is usually used for updating a resource, and delete deletes a resource. The main difference between the regular route and the REST route is that the regular route will return you HTML in most cases, but REST returns data, and in most cases, this is formatted as JSON, but let's look at an example, let's say we want to create a simple user management system. When working with data, three operations are so common that they got their own acronym, it's create, update, and delete, in short, CRUD. So If I want to request all users, I will most probably make a request to /users, and this will give me a JSON with all users in it. If I want to create a user, I will make a post request to this endpoint containing the payload for this user to create it. If I want to get a specific user, I will do a get request to a parameter route which contains maybe something like the user ID, in this case I used 42, so it's the user with ID 42. And if I want to now update this user, I will make a put request which is very similar to the post request, it also contains the payload, but the path I'm using now contains the user ID, so this will now manipulate the user object in the database for instance. Then of course, there's also delete verb that I can use to delete a user with a given ID. REST is very commonly used when creating single-page applications. Simply put, the initial request returns an initial document, this document usually contains a lot of JavaScript which is then used to actually render the HTML or the content into the page. All subsequent interactions with the web sets and the data behind it is done by a so-called XHR, sometimes referred to as AJAX, it's an API built into JavaScript on the browser to do HTTP requests. So after that initial request, the page isn't reloaded anymore, but sends data to the back end when needed and is updated via JavaScript using the data received from the back end. As I said before, there are dedicated courses on REST in the library, but for our task at hand, changing the feedback form to send and receive data via REST, you now know everything that you need to know.

Contents