Npm and XMLHttpRequest for simple GET requests

Until now I have always used plain old XMLHttpRequest for GET requests like

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function() {
  if (this.status === 200) {
    // do something
  } else {
    // do something else
  }
};
xhr.send(null);

      

Now I stumbled upon request and saw that it was extremely popular. Its equivalent

var request = require('request');
request(url, function (error, response, body) {
  if (!error && response.statusCode === 200) {
    // do something
  } else {
    // do something else
  }
})

      

is a bit shorter and I was wondering if there are any significant benefits to it. Perhaps error handling?

+3


source to share


1 answer


The browser is XMLHttpRequest

already built in, so you should only get some other library to build on top of it if that library offers you certain features that you find useful and deserve additional download. In the interest of keeping web pages as fast as possible, I wouldn't just load new modules to do this if you have something built that already works great. So, this is purely a judgment for yourself if the browser and the request module are worth what you get. The request module is a nice flexible and flexible interface that will make it work.

In node.js, it is not an actual inline object XMLHttpRequest

, so whatever this interface offers takes a module like a module http

and then builds a new interface XMLHttpRequest

on top of it.The module request

also builds on a module http

, but it makes it much easier and easier to use (my opinion). So, out of three options: 1) code yourself with the http module, 2) Load the module and XMLHttpRequest code into this interface, or 3) Load the request module and the code to it, you can make your own opinion on which is the cleanest and simplest way coding. Problem loading new modules on the server side (as they are usually loaded once from the local hard drive at startup). I personally believe that the modulerequest()

really simple and very functional, which is why I am using server side HTTP requests. The XMLHttpRequest object never seemed like a great interface in the browser, so I'm not interested in using it on the server.



One of the advantages of a module request()

is that it follows the node.js async callback convention, which means you can use it very easily with promises (one call to something like Bluebird .promisifyAll()

on the module will get you the promised versions of the entire interface ), which many (myself included) are very useful for managing asynchronous responses. The object XMLHttpRequest

does not follow this convention, so the wrapper must be wrapped for clean use with promises (client side jQuery.ajax()

is an example of such a wrapper around XMLHttpRequest

).

If someone were looking for the same interface on client and server, I would probably go with the module request()

just because it seems cleaner to me than the XMLHttpRequest interface (my own opinion).

+4


source







All Articles