无法使用 Express 正确提供压缩文件
Can't serve gzipped files properly using Express
我已经 gzip 了 webpack 包,当我尝试提供它时,我在客户端收到 ERR_CONTENT_DECODING_FAILED
错误。
这是我的中间件:
`app.get('*.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
next();
});`
有人知道发生了什么事吗?我试过使用 .header()
或 .setHeader()
方法,但也没有得到任何想要的结果。
提前致谢。
这里是压缩插件:
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$/,
threshold: 10240,
minRatio: 0.8
})
我只是通过放置这个中间件声明就解决了这个问题:
app.get('*.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
console.log('sent')
next();
});
在 app.use('/static', Express.static('dist'));
之前。 (之前是反之)
我已经 gzip 了 webpack 包,当我尝试提供它时,我在客户端收到 ERR_CONTENT_DECODING_FAILED
错误。
这是我的中间件:
`app.get('*.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
next();
});`
有人知道发生了什么事吗?我试过使用 .header()
或 .setHeader()
方法,但也没有得到任何想要的结果。
提前致谢。
这里是压缩插件:
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$/,
threshold: 10240,
minRatio: 0.8
})
我只是通过放置这个中间件声明就解决了这个问题:
app.get('*.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
console.log('sent')
next();
});
在 app.use('/static', Express.static('dist'));
之前。 (之前是反之)