expressjs / serve-static

Serve static files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apply a transform stream

thom4parisot opened this issue · comments

Hi there,

I was wondering if you envisioned the ability to apply a transform stream to a served file?

I would like to replace some tokens in a static page, eg with stream-replace. I imagined it working like this:

var replace = require('stream-replace');

var serve = serveStatic('public/ftp', {
  'index': ['index.html', 'index.htm'],
  transforms: [
    replace('{{TOKEN}}', process.env.SECRET_TOKEN) 
  ]
})

Instead of having stream.pipe(res), it would be rather like:

transforms.reduce((s, t) => s.pipe(t),stream).pipe(res);

What do you think?

Hi @oncletom in that case, you would no longer be serving static content, as it's now dynamic to the transformation, so it falls out of the vision of this module :)

You can, of course, always add middleware to transform the outgoing response, similar to how compression and similar modules work, though that's probably not as efficient.

@dougwilson Sorry I added an example right after.

I understand it addresses static content but I don't see anything being in the middle of having static and dynamic content.

I don't see anything being in the middle of having static and dynamic content

There is a lot in the middle, as this module provides a lot of functionality. For example, the .pipe method is not from Node.js streams, even though it has the same name, so you cannot give it any other argument than res; this module calculated ETags, which won't work with transformations, the Last-Modified header wouldn't make sense any longer; this module understands byte ranged requests, and with transformations, that wouldn't work any longer; any much more.

Also, this module was named serve-static to do one thing and one thing well: serve static content, rather than being a kitchen sink for any file serving off a file system.

OK understood. Indeed it is not needed to add the transform in this module then.

I thought I could not intercept the response after the static middleware :-)
Thanks a lot for the help!