【发布时间】:2011-04-24 14:41:57
【问题描述】:
我目前正在处理一个项目,该项目需要在内容发送回浏览器之前对其进行 gZip 处理。
我目前使用简单的读取流并将数据传送到请求的响应中,但我不确定在不阻塞请求的情况下使用 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 压缩。
谢谢
【问题讨论】:
-
似乎没有太多关于 gzip 的信息。我知道的模块无法安装,其他的都没有用,我仍然不知道如何实现它。我希望有人回答你的问题。也尽量不要一直调用require,使用闭包。
-
require 行为作为一个注册表,它只是返回预加载的模块,如果我将它分配给一个变量,那将是完全相同的,除了明显创建一个变量之外,所有内容都被引用见