3

I need to implement compression to reduce the size of files fetched by browser. I have pages on the node.js server that read from public folder (JSON and CSV files).

When I visit the pages and analyze them using PageSpeed it suggests :

Compressing the following resources with gzip could reduce their transfer size by ...

... localhost:3000/data/xyz.csv could save 1.1MiB (83% reduction).

How do i serve compressed files to the client. Do I have to compress them beforehand and add it to compressed folder. I make some CSV files on the fly so can i do this in run time.

How do i intercept request for a file and send its compressed content (only xyz.CSV not others).

GET /data/xyz.csv 200 561ms

if i want to compress not all but only select few files say only those in a folder.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • You can use gzip compression. this might help you: http://stackoverflow.com/questions/3894794/node-js-gzip-compression – Emil Condrea Dec 26 '12 at 13:15
  • Thanks, the zlib module should do for on the fly compressing. But how do you serve the already compressed files. I have compressed xyz.csv to xyz.csv.zip myself, how do I serve it. Would I have to decompress it on client end or browser understands the compression and gives me the uncompressed file. – user568109 Dec 27 '12 at 05:47
  • if browser accepts compression it uncompress it automatically – Emil Condrea Dec 27 '12 at 08:30

1 Answers1

4

After a lot of searching I found the easiest option to use the express-zip module

https://npmjs.org/package/express-zip

It does not require any change on developer part (compress/decompress files). Just serve files like you do in express (which is what I use)

user568109
  • 47,225
  • 17
  • 99
  • 123