4

I have some files, from a Unity build, that I am unable to add headers to. They have extensions jsgz, memgz and datagz. They are located in my public folder within my NodeJs project. I am using Express 4 and have set up Compression but I believe that this only compresses existing files for transmission, and does not handle files that are already compressed. I have tried using app.get to add headers but it doesn't seem to work:

app.get('/blah/unitymodels/Release/widget.js', function(req, res, next) {
  ... Check ['accept-encoding'] ...
  if (acceptsGzip) {
      var gzippedPath = req.url + 'gz';
      res.sendFile(gzippedPath, {
          root: './public',
          headers: {
              'Content-Encoding': 'gzip',
              'Content-Type': 'application/javascript'
          }
  }
...

I have tried setting the headers like this, by using res.set and by setting them first then letting the next() call handle the response but whenever I get the file back it is just the gzip file without the extra headers and the browser does not understand it. The approaches I have tried do add other headers ('wibble', 'x-timestamp', etc) so I assume that something else is intercepting these specific ones. How am I able to return these gzipped files so that the browser understands them?

2
  • I think I've fixed this myself. I used express.static to modify the headers, which worked fine. I then used setHeaders inside the app.get function above and this seems to work too. Hopefully it is now fixed. Commented Mar 3, 2016 at 15:21
  • Can you explain a bit more about the static @Matt_JD ? Commented May 31, 2017 at 13:10

1 Answer 1

2

You can use express-static-gzip as Express middleware as shown below:

/* here serves gzipped static files */
app.use('/my_static/zipped/', expressStaticGzip('my_static/zipped/', {
    customCompressions: [{
        encodingName: "gzip",
        fileExtension: "gz"
    }]
}));

/* here serves other static files */
app.use('/my_static', express.static('my_static'));
Sign up to request clarification or add additional context in comments.

1 Comment

This sets for every file the content encoding header to 'gzip'. How do set it just for files with the file-extension 'gz'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.