问题
Question:
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?)
Example:
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).
Edit:
The two first answers make me even more confused as they are perfectly antagonistic.
回答1:
3 different concepts here:
- Resource:
{id: 42, type: employee, company: 5}
- Route:
localhost:8080/employees/42
- Endpoint:
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
回答2:
Endpoints are basically use to perform specific task and return data and endpoints are kind of part of a route.
For example is route and this is also a route but here both of them are returning different data not he same so, we can say that the last two parameter here is kind of end point means the id and question string.
endpoints:
/56075017/difference-between-route-and-endpoint
/56040846/how-to-use-the-classweight-option-of-model-fit-in-tensorflow-js
route:
https://stackoverflow.com/questions/56075017/difference-between-route-and-endpoint
https://stackoverflow.com/questions/56040846/how-to-use-the-classweight-option-of-model-fit-in-tensorflow-js
回答3:
Endpoint: Endpoint is a URL which is used to perform a specific task or functionality.(eg:https://localhost:3000/route)
Route: It is a part of URL endpoint that routes the pages to different components.(eg: https://localhost:3000/thisIsRoute)
回答4:
Routes and endpoints are associated concepts - you can't really have one without the other.
What is an endpoint?
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.
What is a route?
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:
- Express.js - Routing
- Wordpress Developer Resources - Routes and Endpoints
- When to use "client-side routing" or "server-side routing"?
来源:https://stackoverflow.com/questions/56075017/difference-between-route-and-endpoint