1

My route return only html as text , instead of getting the .gz file and providing it as javascript or css:

    router.get('/', function (req, res, next) {
 try {
  res.setHeader('Content-Type', 'text/event-stream')
  res.setHeader('Cache-Control', 'no-cache')

// send a ping approx every 2 seconds
  var timer = setInterval(function () {
   res.write('data: ping\n\n')

   // !!! this is the important part
   res.flush()
  }, 2000)

  res.on('close', function () {
   clearInterval(timer)
  })

  res.render('index', mergedData.globals);

     }
 catch (err) {
  res.render('error', {message: clientData.globals.globalErrorMessage});
  //debug.log ?
 }
});
user254197
  • 883
  • 2
  • 9
  • 31

1 Answers1

0

From what I could understand, you're trying to use gzip encoding for your html/css/javascript. The easiest way of doing that in express is with the compression middleware. After downloading it, and adding it to you dependencies, append it to express as so:

var compression = require('compression');

app.use(compression());

The server will now compress your files before sending them to the client.

Alternatively, if you are using your own solution for compressing the files you have to remember to set the Content-Encoding header to gzip as so: res.setHeader('Content-Encoding', 'gzip');

Matas Kairaitis
  • 326
  • 2
  • 7
  • this was my previous post http://stackoverflow.com/questions/32283346/gzip-not-working i was doing it like you wrote, but it fit not work for me (plus I redirect all to https, so i decided to put it not on top where app.js is, but inside the routes) – user254197 Aug 29 '15 at 16:19