简体   繁体   English

在api请求快递服务器nodejs中上传gzip文件

[英]upload gzip file in api request express server nodejs

I want to upload 'content-encoding:gzip' gzip file using express server in nodejs.我想使用 nodejs 中的 express 服务器上传“content-encoding:gzip”gzip 文件。 I want to know what is the npm module I can use for better performance to receive and uncompress gzip file in request.我想知道什么是 npm 模块,我可以使用它来获得更好的性能来接收和解压缩请求中的 gzip 文件。

there are two methods to handle this.有两种方法可以处理这个问题。 first第一的

npm i compression --save

then in your server.js然后在你的 server.js

const compression = require("compression");
server.use(compression());//make sure you use this  on top of the middlewares

Express middleware will compress your files at the point of request. Express 中间件将在请求时压缩您的文件。 Another solution is to let Webpack compress your files and create .gz versions of your uncompressed files.另一种解决方案是让 Webpack 压缩您的文件并创建未压缩文件的 .gz 版本。

npm i --save compression-webpack-plugin

this package uses "gzip" algorithm by default这个包默认使用“gzip”算法

in your webpack.config.js for production在您的 webpack.config.js 中用于生产

const CompressionPlugin = require("compression-webpack-plugin");

plugins: [
      new CompressionPlugin(),
    ]

this will compress your assets files except jpeg images.这将压缩您的资产文件,除了 jpeg 图像。 because jpeg is already a compressed file type.因为 jpeg 已经是一种压缩文件类型。 now you need to tell express how to handle compressed files.现在你需要告诉 express 如何处理压缩文件。 for this install对于这次安装

  npm i express-static-gzip

in your server.js在你的 server.js 中

 const expressStaticGzip = require("express-static-gzip");
    server.use(expressStaticGzip("dist"));//make sure you use this middleware first.

Webpack compression will compress your files one-time - during your build run. Webpack 压缩将一次性压缩您的文件 - 在您的构建运行期间。 Those compressed versions are then saved to disk然后将这些压缩版本保存到磁盘

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM