Node.js 如何对内容进行gZip即';s通过管道读取流

Node.js 如何对内容进行gZip即';s通过管道读取流,node.js,gzip,piping,Node.js,Gzip,Piping,我目前正在处理一个项目,该项目要求在将内容发送回浏览器之前对其进行gZip加密 我目前正在使用一个简单的读取流并将数据传输到请求的响应中,但我不确定在不阻塞请求的情况下访问gZip内容的最佳方式 发送数据的行是: require('fs').createReadStream(self.staticPath + Request.url).pipe(Response); 请参见下面的类是静态处理程序对象: (function(){ var StaticFeeder = function(

我目前正在处理一个项目,该项目要求在将内容发送回浏览器之前对其进行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压缩


谢谢

事实上,我有一篇关于这件事的博文

您需要:

npm install compress -g
但在使用它之前

其基本思想是使用管道添加功能


然而,对于您的用例,您最好将node.js放在nginx后面来完成所有gzip'ing,因为node.js是一个进程(实际上不是),而gzip例程将占用您的进程CPU。

事实上,我有一篇关于这件事的博客文章

您需要:

npm install compress -g
但在使用它之前

其基本思想是使用管道添加功能


但是,对于您的用例,您最好将node.js放在nginx后面来执行所有gzip'ing,因为node.js是一个进程(实际上不是),而gzip例程将占用您的进程的CPU。

您可以通过压缩流对其进行管道传输:

var fs = require('fs')
var zlib = require('zlib')

fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(Response)

假设文件尚未压缩,并且您已经为响应设置了所有标题。

您可以通过压缩流对其进行管道传输:

var fs = require('fs')
var zlib = require('zlib')

fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(Response)

假设文件尚未压缩,并且您已经为响应设置了所有头。

关于gzip的信息似乎不多。我知道的模块无法安装,其他模块没有用处,我仍然不知道如何实现它。我希望有人能回答你的问题。也试着不要一直调用require,使用闭包。require作为一个注册表,它只返回预加载的模块,如果我将它分配给一个变量,它将是完全相同的,除了明显创建一个变量外,所有内容都被引用,似乎gzip上没有太多信息。我知道的模块无法安装,其他模块没有用处,我仍然不知道如何实现它。我希望有人能回答你的问题。也试着不要一直调用require,使用闭包。require作为一个注册表,它只返回预加载的模块,如果我将它分配给一个变量,它将是完全相同的,除了显然创建一个变量,所有内容都被引用,请参见