Yeah, definitely what @PortableStick says - they key advantage of fetch
and axios
is that you don’t have to mentally translate between front- and back-end code for HTTP requests, you only have to learn how they work once, then you can apply them everywhere.
Just as an addendum, all of them are just a way to map JavaScript data structures to the form expected by the HTTP protocol, then deal with the response a without having to deal with the nitty-gritty of what the response actually looks like, and b give you a nice response mapped back to something that can be dealt with in a way JS understands.
So like (pseudocode):
superAwesomeHTTP({
url: 'http://www.example.com/index.html',
method: 'GET',
headers: {}
}).then(response => console.log(response));
Which will build into something like
GET /index.html HTTP/1.1
Host: www.example.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
And then back comes something like
HTTP/1.1 200 OK
Date: Mon, 16 Jul 2018 14:54:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Mon, 9 Jul 2018 10:25:26 GMT
ETag: "10000000565a5-2c-3e94b66c2e680"
Accept-Ranges: bytes
Content-Length: 44
Connection: close
Content-Type: text/html
<html><body><h1>It works!</h1></body></html>
And superAwesomeHTTP
will parse that and then console log <html><body><h1>It works!</h1></body></html>
.
Node has a relatively good overview of the process here, but it kinda expects you know how it’s all working. This is pretty good though:
https://robrich.org/slides/anatomy_of_a_web_request