Sush
Sush

Reputation: 1457

get page data from html page using node.js

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

Upvotes: 0

Views: 5992

Answers (1)

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

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

Upvotes: 2

Related Questions