在 node.js 中提供压缩构建
Serve gzipped build in node.js
我正在尝试使用 gzip 部署我的构建(我正在使用 'express-static-gzip'),但我仍然不知道如何正确执行。这是我到目前为止想出的:
const express = require('express');
const path = require('path');
const app = express();
const expressStaticGzip = require('express-static-gzip');
app.use(
expressStaticGzip(__dirname),
);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'tvshows.tar.gz/.', 'index.html'));
});
app.listen(3000);
压缩后的文件当然在主目录中。
这是我遇到麻烦的 sendFile。我如何从 gzip 服务我的 index.html?
根据 documentation,您应该在要静态表达的目录中创建一个 index.html.gz
- 在您的例子中,该目录是应用程序目录 (__dirname
)。
因此具有以下文件夹结构
- app-dir
-- index.html.gz (each file has to be gzipped separately)
-- app.js
和
app.use("/", expressStaticGzip(__dirname)); // there's no need for adding a handler and manually using res.sendFile(..)
应该可以解决这个问题。
我正在尝试使用 gzip 部署我的构建(我正在使用 'express-static-gzip'),但我仍然不知道如何正确执行。这是我到目前为止想出的:
const express = require('express');
const path = require('path');
const app = express();
const expressStaticGzip = require('express-static-gzip');
app.use(
expressStaticGzip(__dirname),
);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'tvshows.tar.gz/.', 'index.html'));
});
app.listen(3000);
压缩后的文件当然在主目录中。 这是我遇到麻烦的 sendFile。我如何从 gzip 服务我的 index.html?
根据 documentation,您应该在要静态表达的目录中创建一个 index.html.gz
- 在您的例子中,该目录是应用程序目录 (__dirname
)。
因此具有以下文件夹结构
- app-dir
-- index.html.gz (each file has to be gzipped separately)
-- app.js
和
app.use("/", expressStaticGzip(__dirname)); // there's no need for adding a handler and manually using res.sendFile(..)
应该可以解决这个问题。