[英]I deployed my node app on Heroku and want to enable Gzip compression, any tips?
我尝试从以下位置连接代码: 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();
}
在server.js中:
app.get('/', (req, res) => {
//this place
res.render('home.hbs', {
pageTitle: 'Home Page',
welcomeMess: 'Welcome to my Site'
})
}); 错误:path.exists不是函数。 但我无法理解相同的内容并破坏了我的应用程序。
我希望得到一个压缩的文件。 我正在使用快递处理服务器
问题是您使用的代码依赖于旧节点版本。 path.exists
被替换fs.exists
。 您的代码可能像这样(只需最少的更改)
var fs = require("fs");
//...
path.exists(filePath, function(exists) {
//...
}
请注意,此方法已被弃用,如果您不介意外部依赖关系,则应寻找替代方法,使用fs.stat或fs.access甚至path-exists包。 无论如何用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)
}
});
区别在于错误是回调中的第一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.