Get URL Contents in Node.js with Express


Question

How would I go about downloading the contents of a URL in Node when using the Express framework? Basically, I need to complete the Facebook authentication flow, but I can't do this without GETing their OAuth Token URL.

Normally, in PHP, I'd use Curl, but what is the Node equivalent?

1
12
10/14/2011 7:27:44 PM

Accepted Answer

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/index.html'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

http://nodejs.org/docs/v0.4.11/api/http.html#http.get

27
10/14/2011 9:36:07 PM

The problem that you will front is: some webpage loads its contents using JavaScript. Thus, you needs a package, like After-Load which simulates browser's behavior, then gives you the HTML content of that URL .

var afterLoad = require('after-load');
afterLoad('https://google.com', function(html){
   console.log(html);
});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon