express-static-gzip
express-static-gzip copied to clipboard
expressStaticGzip fail to handle single file
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)
...
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
Ok, it's not very urgent, thanks and good luck with your refactoring !
oops, maybe the issue can keep open...
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!
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 :)
@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'],
})
);