0

Please have a look at my code where I'm trying to compress data using connect.compress middleware. How can I parse the JSON string in browser to get the decompressed data? When I try to hit localhost:2080 I'm getting a page loading error.

Client code:

var options = {
host: '127.0.0.1',
port: 2080,
path: "/",
headers:{
'accept-encoding': 'gzip'
}
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
var data = '';
res.on('data', function (chunk) {
data += chunk;  
});
res.on('end', function (chunk) {
var data2 = JSON.parse(data);
console.log(data2.app_id);
});
});

Server Code:

app = connect();
app.use(connect.compress(console.log("compressed data")))
app.use(connectDomain())
.use(connect.query())
.use(connectRoute(function (router) {

router.get('/', function (req, res) {
            var acceptEncoding = req.headers['accept-encoding'];
            
              if (acceptEncoding.match(/\bdeflate\b/)) {
                    res.setHeader('content-encoding', 'deflate');
              } else if (acceptEncoding.match(/\bgzip\b/)) {
                    res.setHeader('content-encoding', 'gzip');
              }
            console.log(res._headers);  
            res.setHeader('Content-Type', 'application/json');
            res.end('{"app_id": "A3000990"}');
            })
            }))
            
.use(function(err, req, res, next) {
    res.end(err.message);
  });

http.createServer(app).listen(2080);

We can't control the browser. It either sends the Accept-encoding: gzip, deflate header or it doesn't. So can we get compress data using connect.compress()?

rgettman
  • 176,041
  • 30
  • 275
  • 357
user87267867
  • 1,409
  • 3
  • 18
  • 25

1 Answers1

0

There's a couple of issues here:

  • you're setting Content-Encoding headers in the server, but also use connect.compress which will also set that header. That could create conflicts, so don't add those headers yourself and let connect.compress handle all the compression;
  • you don't actually send the HTTP request in your client, add req.end() to it;
  • there's no attempt to decompress the compressed data in your client; how to do that can be found here;
Community
  • 1
  • 1
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • We can't control the browser. It either sends the Accept-encoding: gzip, deflate header or it doesn't. So can we get compress data using connect.compress(). – user87267867 Mar 20 '13 at 11:47
  • What do you mean? Always compress no matter what? – robertklep Mar 20 '13 at 13:03
  • Yes always the response should be compressed because we can't control the browser from client side. Our job is to configure the server so it returns zipped content if the browser can handle it, saving bandwidth for everyone. – user87267867 Mar 21 '13 at 03:38
  • That's what `express.compress` does, it will return compressed data if the browser can handle it. – robertklep Mar 21 '13 at 06:03
  • Yes, sorry for the confusion, they are essentially the same. – robertklep Mar 21 '13 at 06:09
  • But in my above code when I removed headers:{ 'accept-encoding': 'gzip' }, the data was not compressed. Y it is so? – user87267867 Mar 21 '13 at 06:11
  • Because that is the way for browsers/clients to tell the server that they support compression. If that header isn't sent, `connect.compress` *has* to assume that compression is not supported. – robertklep Mar 21 '13 at 06:14