0

I am trying to write a script that will visit a script on localhost and parse it in a node.js script. Here's what I got so far:

var http = require('http');

var options = {
    host:'127.0.0.1',
    port:'80',
    path:'/index.php'
}
var a = http.get(options, function(res){
    res.on('data', function(chunk){
        console.log(chunk);
    });
    //console.log(res);
});

But this is all it returns: <Buffer 7b 22 61 77 65 73 6f 6d 65 22 3a 22 79 65 61 68 21 22 7d>

I know it is some sort of stream but I don't know what to do with it.

nkcmr
  • 10,690
  • 25
  • 63
  • 84

1 Answers1

3

You need to set the encoding of the response object to one of 'ascii', 'utf8', or 'base64' before adding listeners to it. For example:

var a = http.get(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) { // no longer emits a Buffer object
        console.log(chunk);
    });
});
abe
  • 1,549
  • 1
  • 11
  • 9