Http-server: gzip support

Created on 8 Dec 2013  路  29Comments  路  Source: http-party/http-server

Would it be possible to have an option for gzip/deflate compression?

new-feature

Most helpful comment

+1

All 29 comments

+1

+1 would like this too

+1 This sure would be sweet!

+1

:+1:

+1

:+1:

:+1:

No news on this ?

:+1:

I got this working locally (as an always-on feature, not configurable) by modifying http-server.js:

  before.push(ecstatic({
    gzip: true, // HACK: I ADDED THIS LINE!
    root: this.root,
    cache: this.cache,
    showDir: this.showDir,
    autoIndex: this.autoIndex,
    defaultExt: this.ext,
    handleError: typeof options.proxy !== 'string'
  }));

ecstatic will check if the accept-encoding header allows compression, and if present will look for a file that matches the requested file name with '.gz' at the end. For example:
this request: http://example.com/foo.css
with this header: Accept-Encoding: gzip, deflate, sdch
will look for a file called foo.css.gz and will serve it w/ the Content-Encoding: gzip HTTP header if it exists

+10

It't really useful.

+1!

Please use the reaction feature to express you'd like to have this feature.

+1

doing an npm install of http-server doesn't seem to pull in the gzip property event after an npm cache clean. Is this feature available in the npm package yet?

Is this feature available in the npm package yet?

Nope. npm info http-server gitHead says the published git commit is 1a8552c5e028bd5500027ee940111133927a4e94. 1a8552c is behind b456b77, which added gzip support.

For what I understand the current --gzip option requires you to produce something.ext.gz by yourself. But what I want is a option that does the compression on the fly, like the express middleware app.use(compression()), just plug in one line and no hassle.

@golopot right now, I'd say that on-the-fly compression is "out of scope" for a static web server--which should really just transmit content with minimal involvement. If you'd still like to discuss it, though, please file a separate issue. Thanks!

To be clear, on-the-fly compression was exactly what I wanted when i originally opened this issue

Good to know @WickyNilliams. At this point, I don't plan on going beyond what's "underneath" in the form of node-ecstatic's support https://github.com/jfhbrook/node-ecstatic#optsgzip

There are few things we're adding here, but mostly I'm wanting to continue to build up from the node-ecstatic foundation. Any sort of server-side content processing would be out-of-scope in that case.

@BigBlueHat i wasn't necessarily looking for on-the-fly compression. As noted in my previous comment
https://github.com/indexzero/http-server/issues/56#issuecomment-141689367
i got it working w/ a hack, but i'd like this to be supported by the server's options instead of having to hack it myself. Can it be added as an option?

@smarts it's been an option since 0.10.0 was released https://github.com/indexzero/http-server/releases/tag/0.10.0 馃槃 If you need something more, perhaps open a new issue so we can discuss that there rather than on this closed issue.

Doh! I now see the pull request that references this issue. Thanks for being patient w/ me @BigBlueHat 馃槄

+100

Seriously, don't understand how hard on-the-fly compression can be, in just a few lines:

var zlib = require('zlib');

pathName = url.parse(request.url).pathname;
var fp = __dirname+"/../.."+pathName; //custom patthing, ignore

var readStream = fs.createReadStream(fp);
readStream.on('open', function (res) {
response.writeHead(200, { 'content-encoding': 'gzip' });
readStream.pipe(zlib.createGzip()).pipe(response);
});

@Nashorn you're more than welcome to turn your suggestion into a pull request!

It's not clear from looking at http-server.js, where I would integrate the above solution which does dynamic gzip compression on responses. I found this area, but not even sure where/how to plug in.

var serverOptions = {
    before: before,
    headers: this.headers,
    onError: function (err, req, res) {
      if (options.logFn) {
        options.logFn(req, res, err);
      }

      res.end();
    }
  };
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Altiano picture Altiano  路  7Comments

isomorphisms picture isomorphisms  路  5Comments

lacivert picture lacivert  路  3Comments

ghost picture ghost  路  4Comments

gswebspace picture gswebspace  路  6Comments