0

Am trying to get some text from an html page using nodejs

here is the url. from this url i want to get the string 0e783d248f0e27408d3a6c043f44f337c54235ce . i tried this way .but not getting any data

var getGitKey = function (context, callback) {
  http.get("gggg/status", function(res) {
      var data = "";
      res.on('data', function (chunk) {
        data += chunk;
      });
      res.on("end", function() {
console.log("DATA-------------------------------");
console.log(data);
        callback(data);
      });
    }).on("error", function() {
     // callback(null);
    });

};

Please help whats wrong with my code

Sush
  • 1,449
  • 8
  • 26
  • 51

1 Answers1

2

I think your code is fine. You just need to make sure that.

  1. http module is required with var http = require('http');
  2. also the anonymous function you have defined and assigned to the variable getGitKey is never invoked. like getGitKey();

the complete code which worked for me is

var http = require("http");
var getGitKey = function (context, callback) {
http.get("http://integration.twosmiles.com/status", function(res) {
    var data = "";
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on("end", function() {
        console.log("DATA-------------------------------");
        console.log(data);
        callback(data);
    });
}).on("error", function() {
    // callback(null);
});

};
getGitKey();

The result was access denied as your page is protected with simple authentication. Same happens if you try to open it on your browser directly also. If you have a username and password to access the page, then you can refer the below SO answer for detail on using http module with basic authentication.

How to use http.client in Node.js if there is basic authorization

Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • yes but data prints nothing.and here is no password acces in my page – Sush Jun 09 '14 at 05:59
  • from my system the url u given is protected by authentication. So for me data was `HTTP Basic: Access denied.`. What exactly are u getting as output. do u get the initial `DATA-----` you are giving in `end` event? – Mithun Satheesh Jun 09 '14 at 07:26