如何在服务器端解压缩 Koa.js 中的压缩请求数据?

How do I uncompress compressed request data in Koa.js at server side?

我正在开发一个 HTTP 驱动的基于 REST 的应用程序,项目的需要是我必须在 [=13] 处接收压缩的 gzipped JSON 数据 =]服务器端。有几个可用的模块演示压缩响应并将它们发回,但我没有找到任何显示 如何解压缩服务器接收到的请求数据的内容。

看起来这可能与 koa-bodyparser. Under the hood koa-bodyparser uses co-body to parse the request body and co-body uses the inflate package to inflates the request body before parsing 开箱即用。

以下代码:

const koa = require('koa');
const app = new koa();
const bodyParser = require('koa-bodyparser');

app.use(bodyParser());

app.use(function(ctx) {
  ctx.body = ctx.request.body.test;
})

app.listen(3000);

和以下请求

curl \
  -H 'content-type: application/json' \
  -H 'Content-Encoding: gzip' \
  -XPOST \
  --data-binary @data.json.gz \
  localhost:3000

带有 gzipped 的 data.json(原始格式如下所示):

{
  "test": "data"
}

一切如期进行。