API Endpoints or Routes✈
Tags: webdev, programming, api, tutorial
Published: 2023-12-15 18:08:43

How often have you heard about endpoints or routes in APIs but found it difficult to grasp? 🙂😑. Come with me😎, as you reach the end of this article, you will feel comfortable whenever you encounter terms like 'endpoints' or 'routes.' Additionally, you'll be able to implement them on your own using Node.js and Express.😊😉..

## What are endpoints or routes?🤷‍♂️😪
Endpoints are the app's units of executable request-handling code. Huhh??🙄🙂.. Simply put, **an endpoint or route is, as its name implies, basically a pathway or road 🚘🚆 that leads to a destination..** That is, as known from the knowledge of what an API is from the first episode of **"Your first backend project"** [_you could check on it, if you missed it_ [Your first backend project - 1](https://dev.to/adjeijrdev/your-first-backend-project-20gl)] **So an API we got to know is a single link that leads you to a specific application <u>_which will allow you to access resources, post(save) resources or utilize a function/processes/methods already implemented in an application_</u>** Hence each of the options would have a path on the parent URL respectively. Cool?😁...

## Routes in skeleton😂😎
Now that we've grasped the essence of routes/endpoints, let's delve into the details and solidify our understanding once and for all! 😎😉 No time to waste here! 😎
Imagine the link to **Amazon** 🎁 as our **API**. Folks 😁, visit our API at [www.amazon.com](www.amazon.com). Clicking on the link takes you to an **APPLICATION** created by someone. Once you land on the Amazon page, you'll discover numerous options within the application, beyond just buying or viewing. It's packed with various functions, and each function is accomplished through a route.
For instance, when you click on 'registry,' a link or route like [www.amazon.com/registries](www.amazon.com/registries) is what guides you to that specific service. Folks, that's a route! 😂😀 Isn't this breathtaking? 😁😂.. So, from the new link [www.amazon.com/registries](www.amazon.com/registries), we can still see **www.amazon.com**, our **API**, and **/registries**. Therefore, **/registries** is our endpoint or route, and **www.amazon.com** is our **<u>application/API/Parent URL</u>**.

## Let's write our Endpoints or Routes too!😉🧐
So, before we dive into writing our codes, let's carefully note how applications work. We interact with an application by <u>requesting</u> the Amazon platform to help us with a little Christmas🛍 shopping. In turn, Amazon <u>responds</u> to us with various items we need for a happy Christmas! 🎆🎄. Carefully get your Christmas stuffs down!😉🚀 from Amazon. so you see we have two operations here: 
- we **Request** and
- Amazon in turn **Respond**
Hence, the first step is to find yourself at a route, initiate a request, and then await a response.😎 Clear? That is all about it. C'mon we've talked plenty no codes🚀.. let's write some codes.👨‍💻

## Implementing our codes
By now, you should be good to go with setting up your backend project. Installing the Express package and configuring it is just a piece of cake for a pro! 😎😉 If you haven't read the initial article, check it out [Your first backend project - 1](https://dev.to/adjeijrdev/your-first-backend-project-20gl)]!
After setup--this is from the previous article


![API setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhl31zqvo76h8l6q31df.png)

## HTTP REQUEST METHODS🚀🪂
Don't be frightened by the heading. It's as simple as it will always be for a pro like you! 😁👨‍💻!. As we have learned from above, we interact with applications by **request**ing an action. 😎 But have you realized that even though you interact with the application by requesting, there are different things you can ask the application to do for you?🤷‍♂️ This could include filling out a form🌠, deleting items from your Google Drive, updating your status, or simply getting an interesting article like this 😁😎 to read😎🤷‍♂️..
So these various activities are called the **METHODS**! So, applications use several methods to implement our requests based on what we desire, but four are mostly used and dominant: GET, POST, DELETE, and PUT. These terms might not be so strange if you think about them in the context of English vocabulary, right? 😉😁. So:

1.**GET** is used to retrieve or read information
2. **POST** is utilized for creating data within an application or feeding it with information, just like filling out a form.
3. **DELETE** is employed to remove or delete information
4. **PUT** is used to update an already existing information.

All Knowledge needed for implementing a route is settled, lets see an anatomy of if it and wrap up!

![Anatomy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19plksy14ujznluqr3gy.png)
Let's break it down and see from the various sections of the code.

1. **app** is the initialized express package we installed for our API setup in the first article.
2. **".get()"** is a **method** called on the **app**([**_const app = express()_**] which is obviously the package necessary for our API project)😉. So it means we could also use **.post()**, **.put()**, **.delete()** **methods**.We could see the method takes in two parameters, which is explained below:

3. **"/read"** is the first parameter in the **METHOD**(not only get(), but for the rest as well) for our route or endpoint! 🪂 That means, in the address bar after inserting our parent URL, we only add "/read" to navigate to this route or endpoint. So, suppose our parent URL/API is www.ourbackend.com. Then this particular endpoint will be www.ourbackend.com/read  🚀. 

4. The second parameter, which is **"(request, response) => { response.send("You want to read an article like the one you are reading?") },"** is always a callback function. This function is invoked immediately when this endpoint is called upon 💻. So, in this case, if our endpoint is indeed www.ourbackend.com/read, when someone calls this endpoint, they will see the **response**: **'You want to read an article like the one you are reading?'** because the application is responding with that! 🖥🚀 This is breathtaking, seasoned pro!👨‍💻🪂. Clear?🥱😎. Fellas, that means the callback function could be written to do anything whatsoever! Like: allowing user to register a form, delete a blog, update a blog, etc.

## Warm up skills😎🚀👨‍💻
Fellas, you've read quite a bit, and now it's time to put your knowledge into action! As a warm-up for the real implementations in the next blog 💬🚀👨‍💻, let's modify the endpoints using various methods like **post()**, **put()**, and others. Keep the same format, but change the message and methods. This practice will help stabilize your understanding. In the next article, we'll delve into real implementations and explore industrial-standard tools for testing. Remember, the web browser can only test **get()** method, so don't miss out on that for anything! 😪😉 Gear up for some hands-on experience to activate your seasoned pro spirit! 😁👨‍💻📲

Don't forget to **react** to the article and **comment** as well! 😎📲 Let's build a community of fellow devs!
see below for the first article!
[Your first backend project - 1](https://dev.to/adjeijrdev/your-first-backend-project-20gl)]