Is it possible to conditionally append Content encoding response header to node.js http-server module?

Viewed 35

I'm trying to test Unity WebGL build locally. I've tried node.js http-server module. It serves uncompressed game made with Unity 2019 without problems.

However, it won't serve game made with 2020, which uses compression. If gzip or br compression is used, http-server does not send proper Content encoding header responses. Unity web player then complains that it can't parse the .gz or .br response, not knowing what it is.

I've tried writing simple js script to run instead of http-server module. However, it works strange, or not at all, and I don't have days to get into node.js.

var fs = require('fs'), http = require('http');
const path = require('path');

http.createServer(function (req, res) {
  fs.readFile(__dirname + req.url, function (err,data) {
    if (err) {
      res.writeHead(404);
      res.end(JSON.stringify(err));
      return;
    }
    
    var ext = path.extname(req.url);
    if (ext == ".br") {
        res.writehead(200, {'content-encoding': 'br'});
    }
    else {
        res.writehead(200);
    }
    
    res.end(data);
  });
}).listen(8080);

Is there any way to make node.js run its http-server module but append Content encoding response header depending on url? Or can I correct this script to achieve desired behaviour?

Thank you!

0 Answers
Related