Nico Schlömer Nico Schlömer - 1 month ago 8
Javascript Question

npm request vs. XMLHttpRequest for simple GET requests

Up until now, I've always used the plain old XMLHttpRequest for GET requests, e.g.,

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);


I now came across request and saw that it's tremendously 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 tad bit shorter, and I was wondering if there are any more significant benefits to it. Error handling perhaps?

Answer

In a browser, XMLHttpRequest is already built in so you should only get some other library that builds on top of it if that library offers you particular features that you find useful and are worth the extra download. In the interest of keeping web pages as lean as possible, I wouldn't just go loading new modules for the heck of it if you have something built in that already works just fine. So, that's purely a judgement for yourself if browserify and the request module is worth what you're getting. The request module is a nice lean and flexible interface so it has that going for it.

On node.js, the is no actual XMLHttpRequest object built-in so anything that offers that interface is taking a module like the http module and then building a new XMLHttpRequest interface on top of it. The request module is also building on the http module, but it's doing it in a much lighterweight and simpler to use way (my opinion). So, of the three choices: 1) code yourself with the http module, 2) Load the XMLHttpRequest module and code to that interface or 3) Load the request module and code to that, you can form your own opinion about what is the cleanest and simplest way to code. There is less issue with loading new modules server-side (since they're generally just loaded once from local hard disk at startup). I personally find the request() module to be really simple and very functional so that's what I use to make server-side http requests. The XMLHttpRequest object has never seemed like a great interface in the browser so I have no interest in using it server-side.

One advantage of the request() module is that it follows the node.js async callback convention which means you can very easily use it with promises (one call to something like the Bluebird .promisifyAll() on the module will get you promisified versions of the whole interface) which many (myself included) find very useful for managing async responses cleanly. The XMLHttpRequest object does not follow such a convention so a wrapper would have to be put around it to cleanly use it with promises (on the client-side, jQuery.ajax() is an example of such a wrapper around XMLHttpRequest).

If one was looking for the exact same interface on both client and server, I'd probably go with the request() module just because it seems cleaner to me than the XMLHttpRequest interface (my personal opinion).