Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

compress/gzip/deflate streaming content using res.write

compress/gzip/deflate streaming content using res.write

Problem

var express = require('express')

var app = module.exports = express()

function getImages(callback) {
  callback()
}

app
.set('views', __dirname + '/views')
.set('view engine', 'jade')
.get('/stream/images', function(req, res) {
  res.render('layout', function(err, body) {
    res.type('html')
    res.write(body.replace('

', '')) getImages(function(err) { res.render('streams/images', function(err, body) { var html = '

' + body + '

' res.write( "" ) res.write("") res.write('

') res.end() }) }) }) }) .listen(3013)

This works exactly as intended. However, when I .use(express.compress()) (before .get to be exact), the page no longer streams. In other words, the express.compress() seems to wait until the response is finished, then gzip the entire response, then send it. I would like each res.write to send a gzipped response (or specifically, every drain). How can I compress the response correctly?

Edit 1 - I tried this:

var stream = zlib.createGzip()
stream.pipe(res)

stream.write(/* stuff */)
stream.end()

This works exactly like express.compress(). I'm not sure whether the browser just doesn't parse the body until it has the whole thing, or it doesnt send the body until the response is complete.

Problem courtesy of: Jonathan Ong

Solution

var stream = zlib.createGzip()
stream._flush = zlib.Z_SYNC_FLUSH
stream.pipe(res)

stream.write(/* stuff */)
stream.end()
Solution courtesy of: Jonathan Ong

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

compress/gzip/deflate streaming content using res.write

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×