使用命令创建了一个标准的 NestJS 项目:
nest new project-name
。之后,我安装了压缩npm i --save compression
并将其插入项目中。
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as compression from 'compression';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(compression());
await app.listen(3000);
}
bootstrap();
但是压缩不起作用,响应没有
gzip
。
我已经尝试过清除缓存、重新启动浏览器,但没有任何效果。
感谢您的帮助
首先,安装以下软件包:
npm i --save compression
在
main.ts文件中的
bootstrap function
中,应用压缩中间件,如下
async function bootstrap() {
const app: INestApplication = await NestFactory.create(AppModule);
...
app.use(compression({
filter: () => { return true },
threshold: 0
}));
...
await app.listen(4100);
}
然后你就会看到结果了
Control+Shift+R 或 Shift + F5 = 重新加载当前页面,忽略缓存内容。
如果其他方法都不起作用:
const expressApp = express();
expressApp.use(compression());
const expressAdapter = new ExpressAdapter(expressApp);
const app = await NestFactory.create(AppModule, expressAdapter);
await app.listen(3000);