0

I'm so confused right now. I've tried everything and I still cannot get this to work. Basically, I'm trying to get the HTML of a website, but it only works on Google. Here's my code:

const HttpClient = {
    get: function(url, callback) {
        var str = '';
        var req = http.get(url, function(response) {
            response.on('data', function (chunk) {
                str += chunk;
            });
            response.on('end', function () {
                callback(str);
            });
        }).on;
        return true
    }
}
HttpClient.get('http://www.google.com/index.html', (data) => {
    console.log(data);
});

You can see that the URL I am providing is google.com. This is the only URL that I have found that actually returns something. Replacing the URL with anything else will make it not return anything.

Does anyone know why this is happening?

3
  • Can you please add the URLs that didn't work for you? Commented Aug 1, 2017 at 4:40
  • I tried youtube and the netflix api, neither worked. @ManasJayanth Commented Aug 1, 2017 at 4:46
  • Those apis usually use https. Are you sure you are using node's https module? Commented Aug 1, 2017 at 5:18

1 Answer 1

1

Click on the blue 'Run code snippet' button and inspect the error message.

{ "message": "Uncaught ReferenceError: http is not defined", "filename": "https://stacksnippets.net/js", "lineno": 23, "colno": 11 }

You need to make sure you're requiring node's http module.

I was able to successfully make a GET request using your exact code (albeit with a different url) and after requiring the http module.

Updated to use https:

const https = require('https')

const HttpClient = {
    get: function(url, callback) {
        var str = '';
        var req = https.get(url, function(response) {
            response.on('data', function (chunk) {
                str += chunk;
            });
            response.on('end', function () {
                callback(str);
            });
        })

        return true
    }
}

HttpClient.get('https://www.youtube.com/watch?v=oRvwlX3ihRg', (data) => {
    console.log(data)
}, (error) => {
    console.log(error)
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, that URL seems to work. Try youtube.com, it didn't work for me. Also, I forgot to include http, but I did declare it.
YouTube.com uses https, so you'll need to require the https module, see updated answer.
@Josh facebook.com still doesn't work with this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.