Pipe to http server's response and get end (Internet Explorer)

98 views
Skip to first unread message

Jan Maybach

unread,
Feb 10, 2015, 7:40:01 AM2/10/15
to nod...@googlegroups.com
I am struggling with this code:

All with:
                                http.createServer( function( req,res) {}
wrapped around...               

If I do this, Chrome and Internet Explorer keep blank:

                                var raw = fs.createReadStream(filePath);
        var encoding = 'deflate' ;
        raw.pipe(zlib.createDeflate()).pipe(res);
        res.writeHead(200, {
  'content-encoding' : encoding, 
  'Content-Type': contentType + '; charset=utf-8' 
});
res.end()

If I do this, Chrome loads, IE fails:

                                var raw = fs.createReadStream(filePath);
         var encoding = 'deflate' ;
         raw.pipe(zlib.createDeflate()).pipe(res);
         res.writeHead(200, {
  'content-encoding' : encoding, 
  'Content-Type': contentType + '; charset=utf-8' 
});
If I do this, there is no "end" event on the "res"

                                var raw = fs.createReadStream(filePath);
         var encoding = 'deflate' ;
         raw.pipe(zlib.createDeflate()).pipe(res);
         res.writeHead(200, {
  'content-encoding' : encoding, 
  'Content-Type': contentType + '; charset=utf-8' 
});
res.on('end', res.end )

If I do this, the server crashes with:
  -  http.js:919

    this._implicitHeader();

         ^

TypeError: Object #<ReadStream> has no method '_implicitHeader'

    at ReadStream.OutgoingMessage.end (http.js:919:10)

    at ReadStream.emit (events.js:117:20)

    at _stream_readable.js:944:16

    at process._tickCallback (node.js:442:13)

                                var raw = fs.createReadStream(filePath);
         var encoding = 'deflate' ;
         raw.pipe(zlib.createDeflate()).pipe(res);
         res.writeHead(200, {
  'content-encoding' : encoding, 
  'Content-Type': contentType + '; charset=utf-8' 
});
raw.on('end', res.end )


I am in the know about the Node.js documentation, where it says:
  -  "The method, response.end(), MUST be called on each response."
http://nodejs.org/api/http.html#http_response_end_data_encoding_callback
...


What can I do?


Floby

unread,
Feb 11, 2015, 8:37:10 AM2/11/15
to nod...@googlegroups.com
`pipe()` calls `end()` for you a the end of the source stream. So that leaves solution 1 with  mistake.

res is a writable stream and therefore does not emit "end" events.

I would suggest write the HEAD of your response before piping to it.

                                var raw = fs.createReadStream(filePath);
         var encoding = 'deflate' ;
         res.writeHead(200, {
  'content-encoding' : encoding, 
  'Content-Type': contentType + '; charset=utf-8' 
});
                                raw.pipe(zlib.createDeflate()).pipe(res);


If IE fails, then you probably need to tell us which version of IE you are targetting. I would suppose some IEs don't work well with deflate and prefer gzip, and there might also be some problems with chunked encoding.
Reply all
Reply to author
Forward
0 new messages