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.

0

1 Answer 1

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);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

or just use chunk.toString() or chunk.toString('utf8')
@AndreySidorov Doing that can break multibyte characters.

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.