I have a probably rather simple question, but I'm unable to find an answer with nice explanations:
What is the difference (if any) between a route and an endpoint in the context of a RESTful API developed within a Node.js
/ Express
application (but these concepts may be broader?!)?
(Does it relate to URLs in some way?)
For example, in this article: https://medium.com/@purposenigeria/build-a-restful-api-with-node-js-and-express-js-d7e59c7a3dfb we can read:
We imported express which we installed at the beginning of the course, app.get makes a get request to the server with the route/endpoint provided as the first parameter, the endpoint is meant to return all the todos in the database.
These concepts are used interchangeably, which makes me confused.
(please note that I'm a 100% beginner with REST API, nodejs and express but I try to do my best to learn).
The two first answers chronologically speaking make me even more confused as they are perfectly antagonistic.
API is usually a definition term, Endpoint or route are physical representation. When somebody says "build an API" that means you have to define its specification e.g. protocol, request/response schema, (may be) security credentials and (of course) an endpoint to hit.
Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path—for example, GET /pets . You can define specific HTTP methods for your route. Or, you can use the ANY method to match all methods that you haven't defined for a resource.
It's important to note that endpoints and APIs are different. An endpoint is a component of an API, while an API is a set of rules that allow two applications to share resources. Endpoints are the locations of the resources, and the API uses endpoint URLs to retrieve the requested resources.
A route can be part of a path when only a section of the path is actually traveled. A path is always physical available and may end in a rather unclear manner (e.g A footpath in a forest that blurs away). One or more paths can be part of a route.
3 different concepts here:
{id: 42, type: employee, company: 5}
localhost:8080/employees/42
GET localhost:8080/employees/42
You can have different endpoints for the same route, such as DELETE localhost:8080/employees/42
. So endpoints are basically actions.
Also you can access the same resource by different routes such as localhost:8080/companies/5/employees/42
. So a route is a way to locate a resource.
Read more: Endpoint vs. route
Read more: Endpoint vs. resource
http://www.mywebsite.com/
GET http://www.mywebsite.com/Products
A Route is the URI, and the Endpoint is the action performed on the URI.
Routes and endpoints are associated concepts - you can't really have one without the other.
Generally speaking, an "endpoint" is one end of a communication channel where one system interacts with another system. This term is also used similarly in networking.
For a typical web API, endpoints are URLs, and they are described in the API's documentation so programmers know how to use/consume them. For example, a particular web API may have this endpoint:
GET https://my-api.com/Library/Books
This would return a list of all books in the library.
A "route" is typically code that matches incoming request paths to resources. In other words, it defines the URL and what code will be executed. A route path might contain regular expressions, patterns, parameters, and involve validation. For example, consider this route path:
"{controller}/{action}/{id?}"
In ASP.NET, pattern matching is applied, so GET https://my-api.com/Library/Books/341
would call the Books
public method on the Library
class, passing a parameter of 341
. Routing frameworks can be very flexible and versatile.
The simplest example of an endpoint is to put a file you want to be consumed (say data.json
) inside the public_html
folder of your web server. It can be reached by GET https://my-api.com/data.json
. The routing is handled by the web server out of the box and no routing code is required.
Some good things to read next:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With