2017-08-06 30 views
0

我有这段代码;如何在express中压缩fs.createReadStream

const express = require('express') 
const compression = require('compression') 
const app = express() 
const fs = require('fs'); 
const path = require('path'); 
const port = process.env.PORT || 3003; 

app.use(compression()) 
app.use('/public', express.static('public')); 

app.get('/', (req, res) => { 
    fs.createReadStream(__dirname + '/public/src/index/index.html').pipe(res); 
}); 

app.listen(port,() => { 
    console.log(`Portfolio listening on port ${port}`) 
}) 

在铬开发工具被请求在index.htmlapp.js文件gzip压缩,但该第一文件(index.html在上面的代码称为)不是。

如何在流式传输之前压缩index.html文件?

+1

我认为使用压缩中间件应该已经照顾那个。你如何验证压缩失踪? – jsalonen

+0

在chrome开发工具中,index.html文件的内容编码为空,但表示JS文件为gzip。 – leonormes

回答

1

如何使用zlib模块:

fs.createReadStream(__dirname + '/public/src/index/index.html') 
    .pipe(zlib.createGzip()) 
    .pipe(res);