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.

3
  • You can use gzip compression. this might help you: stackoverflow.com/questions/3894794/node-js-gzip-compression Commented Dec 26, 2012 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. Commented Dec 27, 2012 at 5:47
  • if browser accepts compression it uncompress it automatically Commented Dec 27, 2012 at 8:30

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.