How to show the data in the console.log() ? (node.js) (SOLVED)

Hi, I am confuse on this,how to make this work…I’m just reading some example but it did not show how to have data in console.log(); or to display the chunk.

//Testread.js

http.createServer(function(request,response){
    response.writeHead(200);
    request.on('data',function(chunk){
        console.log(chunk.toString());
    });

    request.on('end',function(){
        response.end();
    });

}).listen(1337,'127.0.0.1');

console.log('Server running at  http://127.0.0.1:1337/');

I know how to run this in terminal,but my problem is that I don’t know how to put value in the chunk data.

Thank you in advance.

console.log(chunk); 

Should work I think. I’m fairly new to Node.js myself.

Why aren’t you using a framework like Express? You’ll have better luck doing Express tutorials rather than trying to do it all manually directly in Node.

Thank you for the reply,…I know that will work the code that i posted, I just want to ask when will it receive the data or the chunk ?..I am opening in the browser 127.0.0.1:1337 but nothing happens.

Thank you in advance.

Ok it works now…I understand lil bit using this node.js :smile:

data is the event.

http://nodejs.org/api/stream.html#stream_event_data

Event: ‘data’#

chunk Buffer | String The chunk of data.
If you attach a data event listener, then it will switch the stream into flowing mode, and data will be passed to your handler as soon as it is available.

If you just want to get all the data out of the stream as fast as possible, this is the best way to do so.

The chunk is the string buffer. It is partial data.

:smiley: Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.