express-static-gzip icon indicating copy to clipboard operation
express-static-gzip copied to clipboard

expressStaticGzip fail to handle single file

Open FranckFreiburger opened this issue 7 years ago • 6 comments

Like serve-static, express-static-gzip should also be able to serve a single file. eg: expressApp.use('/service-worker.js', expressStaticGzip('service-worker.js'));

Currently, the error is:

Error: ENOTDIR: not a directory, scandir 'C:\test\dist\service-worker.js'
    at Object.fs.readdirSync (fs.js:909:18)
    at findAllCompressionFiles (C:\test\node_modules\express-static-gzip\index.js:127:24)
    at expressStaticGzip (C:\test\node_modules\express-static-gzip\index.js:28:9)
    at serve (C:\test\myserver.js:344:10)
    ...

FranckFreiburger avatar Jul 20 '17 21:07 FranckFreiburger

Hi,

as i wanted to restructure the code of expressStaticGzip for a while now and make it easier to test, i will have a look at your issue while doing so.

At the moment I can not guarantee any timeframe when this will be done, as my main focus is on adding tests to ensure no future changes will break the module. So you might want to provide a pull request for a fix,in case this is an urgent issue you are depending on to be fixed.

Regards, Tobi

tkoenig89 avatar Jul 21 '17 11:07 tkoenig89

Ok, it's not very urgent, thanks and good luck with your refactoring !

FranckFreiburger avatar Jul 21 '17 13:07 FranckFreiburger

oops, maybe the issue can keep open...

FranckFreiburger avatar Jul 21 '17 13:07 FranckFreiburger

There is no point to serve service-worker.js with expressStaticGzip cos you can't gzip it anyway, at least with Webpack's CompressionPlugin.

Does anybody knows the better way to serve * route with 'express-static-gzip' than I pointed below?

app.get('/service-worker.js', (req, res) => {
  res
    .sendFile(
      path
        .resolve(
          __dirname,
          'client',
          'build',
          'service-worker.js'
        )
    )
})

app.use(
  '/',
  expressStaticGzip(
    'client/build'
  )
)

app.use(
  '*',
  expressStaticGzip(
    'client/build'
  )
)

Hope it will be possible to have options to serve single file or array of files, not only the directory.

Thank you for a great plugin!

JustFly1984 avatar Jan 13 '18 17:01 JustFly1984

I think you can reduce your last two middleware registrations to just one like this:

app.use(expressStaticGzip('client/build'))

This should handle all requests, that would not have been handled before. But you might want to have some other request handler at this position, to serve "404" errors.

I tried adding the single file functionality a while ago, but it took me quite some time, and i did not figure out a simple solution at that time. As this was not one of my own use cases, i stopped implementing. Any PR providing such functionality would be welcome :)

tkoenig89 avatar Jan 16 '18 06:01 tkoenig89

@FranckFreiburger you could use app.get instead of app.use because app.get gets mounted on the path and you can't server the file

app.get(
    '/service-worker.m?js',
    expressStaticGzip('build/client', {
      index: false,
      enableBrotli: true,
      orderPreference: ['br'],
    })
  );

kamleshchandnani avatar Jun 20 '19 05:06 kamleshchandnani