How does the browser know if it is the right endpoint method for the request?


How does the browser know if it is the right endpoint method for the request?


When Talking about updating or deleting browsers do not have the support to read the request for the proper method, HTML forms (up to HTML version 4 and XHTML 1) only support GET and POST as HTTP request methods.
But of course, PUT and DELETE are supported through AJAX (XMLHttpRequest) calls, it is like an object tunnel that is able to communicate with a server and pass the parameters as an object or as part of a url string as we have seen in the examples:


So, the browser does not directly know, but that is not the only place where we use an HTTP or HTTPS route, a request from the front end can be made to the route and pass a method value on the request object, that way, for example:

var xhttp = new XMLHttpRequest();"PUT", "http://localhost:4001/expressions/1?name=happy");

or using fetch:

return fetch('http://localhost:4001/expressions/1?name=happy', {method: PUT});

Now, why is this important? Because as mentioned earlier browsers are not equipped to support PUT and DELETE requests, but web HTTP protocols have a method that can make those requests, and there are preset structures to request objects. Express receives those requests and that is where we check the method value in the object and the route, to match it towards the right handler, so even though a browser cannot send the request for an update or a delete action, a method like fetch or the XMLHttpRequest from the window API would send the method type of the request and express will use that information to funnel the request to perform the right action and return a response.