I'm trying to static serve brotli compressed files. I was able to compress files with this code:
// node.js
const compressStream = require("iltorb").compressStream;
const fs = require("fs");
const wrr = fs.createWriteStream("index-b.html");
fs.createReadStream("index.html")
.pipe(compressStream())
.pipe(wrr);
So now the index-b.html
file is compressed. But how can I see this on browser?
When I go to that page, this is what I'm seeing:
I thought the browser will understand this is a brotli encoded file. But it didn't. When I looked at the response headers,
HTTP/1.1 200 OK
server: ecstatic-3.3.1
cache-control: max-age=3600
last-modified: Sat, 06 Apr 2019 17:27:32 GMT
etag: W/"281474976993459-334-2019-04-06T17:27:32.047Z"
content-length: 334
content-type: text/html; charset=UTF-8
Date: Sat, 06 Apr 2019 17:36:49 GMT
Connection: keep-alive
there was no encoding set. How can I address this?
You are using http-server which is a very simple HTTP server and does not allow you to set content-encoding explicitly.
It looks like there is an issue raised to add Brotli support, but it has not been completed yet. Even if it was, it is only intended to serve static, pre-compressed files (assuming it will be implemented the same as the --gzip
option).
To use dynamic brotli like you want, you have to use the normal Node http.createServer code or something like express.