开发者

How to read a gzip file and send out to browser and let the browser decode it in Node.js?

Because of some non-programming restriction, I temporary cannot use the most updated node.js Express and Connect. I have to stick with 2 subversion behind.

So, now, I have to do my only gzip javascript (because of older version doesn't take care of ETag, etc).

Here is what I do: (I had the gzipped file which is a working one)

fs.readFile('my.js.gz', function(err, data) {
  if (!err) {
   gzipJsFile = data;
  }
}


app.get('my.js', function(req, res) {
   // assume req.headers.accept-encoding contains gzip
   res.header('Content-Type', 'text/javascript');
   res.header('Content-Encoding', 'gzip');
   res.header('Accept-Ranges', 'bytes');
   res.heade开发者_StackOverflowr('Transfer-Encoding', 'chunked');
   res.header('Vary', 'Accept-Encoding');
   res.send(gzipJsFile);
});

And the browser cannot unzip it.

I use Friddler. it complains there is Content-Length mismatch. And It also complains the magic number in Gzip header is not correct.

I'm wondering, if I'm doing the correct way to read the gzip file and send it to the HTTP response?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜