2

I am following the simple guide in the npm compression module, but I'm not seeing compression on my static pages. It does look like the pages being redered by Express are being compressed. Is there something I need to do to get the static files compressed as well?

I am using Express 4.2.0 and Compression 1.0.8.

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

Here is one of my router functions:

router.get('/:name', function(req, res) { res.render('test/blah/' + req.params.name, { title : entry.title }); } 
5
  • can you clarify the versions of express and compression? Commented Jul 16, 2014 at 20:27
  • Express is 4.2.0 and Compression is 1.0.8 Commented Jul 16, 2014 at 20:28
  • Showing us one of your request handler functions and the request that you are testing with may help. Commented Jul 16, 2014 at 20:31
  • Updated...hoping that is what you wanted. Commented Jul 16, 2014 at 20:37
  • It looks like the content being rendered by Express is being compressed, but that my static resources under public (JS and CSS files) are not being compressed. Will update question. Commented Jul 16, 2014 at 20:48

1 Answer 1

3

in your middleware chain try to use compress middleware before the static middleware

app.use(compression()); app.use(express.static(__dirname+'/public')); 
Sign up to request clarification or add additional context in comments.

2 Comments

I thought this was working, but the static resources are still not compressed.
I used to use nginx as reverse proxy for nodejs project and static file server, and it compress the output properly. I used this config - github.com/vodolaz095/hunt/blob/master/examples/… i hope it will help you this approach makes you to use nginx as proxy and file server, it can give doubts, but imho nginx works little faster as file server, and it allows nodejs backend to be executed on behalf of limited user account - it can bind to port > 1000, 3000 for example, and nginx will proxy the requests from 80/443 port to 3000 port of nodejs app

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.