我目前正在处理一个项目,该项目需要在将内容发送回浏览器之前对其进行 gZip-ed。
我目前使用简单的读取流并将数据传输到请求的响应中,但我不确定在不阻塞请求的情况下使用 gZip 内容的最佳方式
发送数据的线路是:
require('fs').createReadStream(self.staticPath + Request.url).pipe(Response);
看到下面的类是静态处理程序对象:
(function(){
var StaticFeeder = function()
{
this.staticPath = process.cwd() + '/application/static';
this.contentTypes = require('./contenttypes')
}
StaticFeeder.prototype.handle = function(Request,Response,callback)
{
var self = this;
if(Request.url == '/')
{
return false;
}
if(Request.url.indexOf('../') > -1)
{
return false;
}
require('path').exists(this.staticPath + Request.url,function(isthere){
/*
* If no file exists, pass back to the main handler and return
* */
if(isthere === false)
{
callback(false);
return;
}
/*
* Get the extention if possible
* */
var ext = require('path').extname(Request.url).replace('.','')
/*
* Get the Content-Type
* */
var ctype = self.contentTypes[ext] !== undefined ? self.contentTypes[ext] : 'application/octet-stream';
/*
* Send the Content-Type
* */
Response.setHeader('Content-Type',ctype);
/*
* Create a readable stream and send the file
* */
require('fs').createReadStream(self.staticPath + Request.url).pipe(Response);
/*
* Tell the main handler we have delt with the response
* */
callback(true);
})
}
module.exports = new StaticFeeder();
})();
谁能帮我解决这个问题,我不知道如何告诉管道用 gZip 压缩。
谢谢