简体   繁体   English

我在Heroku上部署了我的节点应用程序,想要启用Gzip压缩,有什么提示吗?

[英]I deployed my node app on Heroku and want to enable Gzip compression, any tips?

I tried to wire up a code from: https://github.com/wimagguc/nodejs-static-http-with-gzip/blob/master/http-with-gzip.js Directories and Server.js File I made changes by adding the code: 我尝试从以下位置连接代码: https : //github.com/wimagguc/nodejs-static-http-with-gzip/blob/master/http-with-gzip.js 目录和Server.js文件我通过以下方式进行了更改添加代码:

path.exists(filePath, function(exists) {

    if (exists) {
        fs.readFile(filePath, function(error, content) {
            if (error) {
                response.writeHead(500);
                response.end();
            }
            else {
                var raw = fs.createReadStream(filePath);

                if (acceptEncoding.match(/\bdeflate\b/)) {
                    response.writeHead(200, { 'content-encoding': 'deflate' });
                    raw.pipe(zlib.createDeflate()).pipe(response);
                } else if (acceptEncoding.match(/\bgzip\b/)) {
                    response.writeHead(200, { 'content-encoding': 'gzip' });
                    raw.pipe(zlib.createGzip()).pipe(response);
                } else {
                    response.writeHead(200, {});
                    raw.pipe(response);
                }
            }
        });
    }
    else {
        response.writeHead(404);
        response.end();
    }

in the server.js at: 在server.js中:

app.get('/', (req, res) => {
//this place
    res.render('home.hbs', {
    pageTitle: 'Home Page',
    welcomeMess: 'Welcome to my Site'
})

}); }); the error: path.exists is not a function. 错误:path.exists不是函数。 but i could not understand the same and broke my app. 但我无法理解相同的内容并破坏了我的应用程序。

i was hoping to get a file that is gzipped. 我希望得到一个压缩的文件。 I am using express for handling the server 我正在使用快递处理服务器

The problem is the code you use relies on old node versions. 问题是您使用的代码依赖于旧节点版本。 path.exists was replaced with fs.exists . path.exists被替换fs.exists You code could be like this (with minimal changes) 您的代码可能像这样(只需最少的更改)

var fs = require("fs");
//...
path.exists(filePath, function(exists) {
    //...
}

Be aware that this method is deprecated and you should look for alternatives either use fs.stat or fs.access or even path-exists package if you don't mind external dependency. 请注意,此方法已被弃用,如果您不介意外部依赖关系,则应寻找替代方法,使用fs.statfs.access甚至path-exists包。 Anyway with fs.access it would be like this 无论如何用fs.access都会是这样

fs.access(filePath, (err) => {
  if (err) {
    response.writeHead(404);
    response.end();
  }else{
    // ... rest of the method (here you know file exists and accessible)
  }      
});

The difference being the error is the first prameter in the callback. 区别在于错误是回调中的第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否应该为WebP图像启用GZIP压缩? - Should I enable GZIP compression for WebP images? 为什么我的 heroku 应用程序在使用 gzip 压缩后加载资源的时间过长? - Why is my heroku app loading resources too long even after using gzip compression? 部署到heroku时,如何使我的node / socket.io应用使用正确的端口? - How do I get my node/socket.io app to use the correct port when deployed to heroku? 为运行在 Azure 上的节点应用程序启用 gzip - Enable gzip for node app running on Azure 我在 Heroku 上部署了我的应用程序,但后来我遇到了 cors 问题 - I deployed my app on Heroku but then I have cors problems 当我在 heroku 中部署我的应用程序时,获取 html 时出错 - Error getting html when I deployed my app in heroku 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。 你可以阅读下面的描述 - my node app is working fine on localhost but when i deployed on heroku , it is giving me application error. you can read description below NodeJS GZip压缩-有问题吗? - NodeJS GZip Compression - Any Issues? 使用nodemon部署到Heroku的Node应用随机崩溃 - Node app deployed to Heroku with nodemon randomly crashes 部署在 Heroku 上的节点应用程序立即崩溃 - Node app deployed on Heroku immediately crashes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM