-
fetch
andXMLHttpRequest
are browser APIs. -
http
is Node-specific (EDIT for clarity:http
as in the Node module with functions relating to dealing with HTTP requests, not http in general) -
fetch
effectively replacesXMLHttpRequest
(with some caveats regarding cancelling requests), as it is much more powerful. -
fetch
returns a promise, which makes things easier in many ways. - the
node-fetch
package provides the same API asfetch
for use server-side and tries to deal with some Node-specific things (streams being the key thing). -
fetch
is very low-level and explicit - you don’t get anything unless you ask for it. For example, I’ve normally had to write a wrapper function that automatically adds in defaults needed by whatever app I’m building (CORs, cookie logic, XSRF protection logic etc). -
http
is also very low-level. -
request
is a library that wrapshttp
and gives you some nice conveniences. -
axios
, when used server side, does the same thing. -
axios
andrequest
, by providing useful defaults, can make things easier to code, with the normal cost of learning another API.
NodeJS: When handling http requests how do you determine which to use? (http, request, fetch, axios)
7 Likes