0
我想在我的快速應用程序中gzip靜態文件以減小腳本的大小。我嘗試使用快速''壓縮'模塊,但它不會壓縮任何東西。我找到的所有答案都只是把app.use(壓縮())放在所有路徑之上,並將壓縮的大小閾值設置爲零,我做了兩件事,但我做了兩件事,米仍然無處可去。Express.js壓縮模塊不工作
下面是相關代碼:
var express = require("express");
var fs = require('fs');
var xml2js = require("xml2js");
var auth = require("./authenticator.js");
var bodyParser = require('body-parser');
var _ = require('lodash');
var compression = require('compression')
var app=express();
app.use(compression({threshold : 0}));
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({
extended: true
});
app.get('/', function(req, res){
res.header('X-UA-Compatible', 'IE=Edge');
res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
sendFile(res, 'index.html', 'text/html');
});
app.get('/testGZIP', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
var msg = JSON.stringify({'text':_.times(5000, _.uniqueId())});
res.write(msg);
res.end();
});
app.use(function(req, res, next) {
var urls = ['modules/', 'font/', 'js/', 'css/', 'views/', 'img/'];
if(_.some(urls, (el) => _.includes(req.url, el))) {
console.log("g-zipping request " + req.url + "...");
res.setHeader("Cache-Control", "public, max-age=604800, must-revalidate");
}
return next();
});
app.use('/font/', express.static('./font/'));
app.use('/modules/', express.static('./node_modules/'));
app.use('/css/', express.static('./css/'));
app.use('/views/', express.static('./views/'));
app.use('/img/', express.static('./img/'));
app.use('/js/', express.static('./js/'));
var server = app.listen(8010, function(){
var host = server.address().address;
var port = server.address().port;
console.log("\nServer started on ip " + host + ' on port ' +port + " - " + (new Date()).getHours() + ":" + (new Date()).getMinutes() + "\n");
});
這裏是標頭/ testGZIP:(任何其他靜態資源返回相同)
的內容,當然,是不是壓縮。
我在做什麼錯?
編輯:
我試着用zlib的,而比gzip這樣做,但不壓縮兩種:
app.get('/testZlib', function(req,res){
res.writeHead(200, {
'Content-Encoding': 'gzip' });
fs.createReadStream('js/client-main.min.js').pipe(zlib.createGzip()).pipe(res);
});
不,這並沒有這樣做 – Hankrecords