Question

For my app, I need to read from another api endpoint, say like facebook. This is what I am doing now:

var https=require('https');
var querystring=require('querystring');
var _my_lat=37.78583526611328;
var _my_lng=122.40641784667969;
var _my_fb_access_token ='somebigvaluefromfacebook';
var params = {
        type: 'place',
        center: _my_lat+','+_my_lng,
        access_token: _my_fb_access_token
};
var url = 'https://graph.facebook.com/search?'+querystring.stringify(params);
https.get(url, function(res) {
    var msg = '';
    res.on('data', function(chunk) {
        msg+=chunk;
    });
    res.on('end', function() {
        var v = JSON.parse(msg);
        //do stuff with v
    });
}).on('error', function(e) {
    //do stuff with e
});

But, I recently found this: https://github.com/mikeal/request and the example from the link is below:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

I am new to Node.js and I could not figure out which one is better for my purpose.

Was it helpful?

Solution

Mikeal Rogers' Request module is, as he states on the web site, "designed to be the simplest way possible to make http calls". If what you need is a simple way to interact with a remote REST API, this is an excellent way to go. If you need to handle HTTP requests at a lower level--for example, if you needed to listen for an HTTP upgrade request--you'll want to use Node's native HTTP module.

Your Facebook code is a good example of something that would be much simpler with the Request module! You can basically swap out the URL in your Google example with your Facebook Graph API URL and go from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top