18

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?

Andrew M
  • 4,128
  • 10
  • 40
  • 67

5 Answers5

30
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

chovy
  • 65,853
  • 48
  • 201
  • 247
  • 2
    Thanks for the pure-Node solution. While the whole event driven thing is cool, the simplicity of the request module makes the code a lot simpler for the project I'm working with. Because I didn't specify module or module-less, I'm going to mark this as the answer. – Andrew M Oct 15 '11 at 19:28
  • 2
    I found the accepted answer at http://stackoverflow.com/questions/6695143/how-to-make-web-service-calls-in-expressjs to be a better example of the same solution. – ashack Feb 13 '13 at 00:25
  • 4
    Um?? The question asked for the HTML, not the status code. – Jack M Sep 20 '18 at 20:16
15

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);
});
Sergio
  • 27,998
  • 10
  • 81
  • 130
Abdennour TOUMI
  • 76,783
  • 33
  • 229
  • 231
6

Using http way requires way more lines of code for just a simple html page .

Here's an efficient way : Use request

var request = require("request");

request({uri: "http://www.sitepoint.com"}, 
    function(error, response, body) {
    console.log(body);
  });
});

Here is the doc for request : https://github.com/request/request



2nd Method using fetch with promises :

    fetch('https://sitepoint.com')
    .then(resp=> resp.text()).then(body => console.log(body)) ; 
Natesh bhat
  • 10,121
  • 9
  • 68
  • 112
0

Using http module:

const http = require('http');

http.get('http://localhost/', (res) => {
    let rawHtml = '';
    res.on('data', (chunk) => { rawHtml += chunk; });
    res.on('end', () => {
        try {
            console.log(rawHtml);
        } catch (e) {
            console.error(e.message);
        }
    });
});

rawHtml - complete html of the page.

I just simplified example from official docs.

Scofield
  • 3,490
  • 1
  • 22
  • 29
-1

using Axios is much simpler

const axios = require("axios").default

const response = axios.get("https://google.com")

console.log(response.data)

or

const axios = require("axios").default

const response = axios.get("https://google.com").then((response)=>{
console.log(response.data)
})

for full docs, you can head over Axios Github