LearningNodeJS icon indicating copy to clipboard operation
LearningNodeJS copied to clipboard

Chapter 6 -- 03 Serve Static content pauses

Open p-donahue opened this issue 8 years ago • 3 comments

The chapter 6 code for "Serve Static Content Pauses" crashes with the error:

TypeError: First argument must be a string or Buffer when it calls: if (!res.write(data))

p-donahue avatar Aug 28 '17 02:08 p-donahue

The function needs the if(data) block used previously.

rs.on('readable', () => {
    var data = rs.read();
    if(data) {
         if (!res.write(data)) {
            rs.pause();
         }
    }
});

I'd love a better explanation of why its needed.

p-donahue avatar Aug 28 '17 03:08 p-donahue

Hi!

Thanks for the note. That's behaviour I haven't seen before. I guess there is now a case where you get a readable, but there's no data to actually read. I suspect the "readable" event is still fired to let you know that it's finished or something.

What version of node is this for? I wonder if it's a change.

marcwan avatar Sep 27 '17 13:09 marcwan

I've the same issue when I try to refresh the page. My node version is v8.3.0

smallbonelu avatar Oct 18 '17 01:10 smallbonelu