NodeJS: When handling http requests how do you determine which to use? (http, request, fetch, axios)

  • fetch and XMLHttpRequest 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 replaces XMLHttpRequest (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 as fetch 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 wraps http and gives you some nice conveniences.
  • axios, when used server side, does the same thing.
  • axios and request, by providing useful defaults, can make things easier to code, with the normal cost of learning another API.
7 Likes