0

I have all my static files under ./static folder without compressing. Is there an out of box library which can compress files in ./static on the fly and cache the result in memory?

Edit:

The reason that I want to compress and cache in memory is for better CDN performance when CloudFlare Miss a cache. I use Traefik as my reverse proxy, which only supports cache for the enterprise version, so I'd like to do it directly in node.

1 Answer 1

0

Better would be to use some third party for compressing and caching. For example nginx can be used for serving and compressing static files. However if you want to keep using NodeJS, here you can view the answer: Express gzip static content. To simplify answer in short

Express 3.0 now has compress() support:

var app = express(); // gzip app.use(express.compress()); // static app.use("/public", express.static(__dirname + '/public')); // listen app.listen(80); 

EDIT for Express 4.0, compress become the separate middleware. So you have to install and import to use it:

var compress = require('compression'); app.use(compress()); 

As for caching you can view this blog: Does express.static() cache files in the memory?. In short, nodeJS does not do caching for you, but client browser side does based on tags and headers you provide. If you want extra caching layer, you can look into CDN's like cloudflare which will not only cache your static files but also serve it from closest server to client

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

1 Comment

Thank you. I have read those answers before asking this question. Those answers are too old. The only solution sounds right, that is gzippo, hasn't been maintained for 10 years. The reason that I want to compress and cache in memory is for better CDN performance when CloudFlare Miss a cache.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.