简体   繁体   English

如何从Google Cloud Functions发送gzipped json响应?

[英]How to send gzipped json response from Google Cloud Functions?

My JSON responses in one of my Google Cloud Functions could be reduced up to 70-80% in size if I respond using gzip compression. 如果我使用gzip压缩响应,我的一个Google Cloud Functions中的JSON响应可以减少70-80%。

How can I send a compressed json response from my Functions (trigger via http(s))? 如何从我的函数发送压缩的json响应(通过http(s)触发)?

This would also mean I would save on a lot of network expenses with google cloud platform, and a faster loading of the data for mobile consumers of the data. 这也意味着我可以通过谷歌云平台节省大量网络费用,并为数据的移动消费者更快地加载数据。

I've tried using the zlib native module but no luck... 我尝试过使用zlib原生模块,但没有运气......

if (req.get('Accept-Encoding') && req.get('Accept-Encoding').indexOf('gzip') > -1) {

    interpretation.gzip = true;

    const zlib = require('zlib');

    res.set('Content-Type', 'text/plain');
    res.set('Content-Encoding', 'gzip');

    zlib.gzip(JSON.stringify(interpretation), function (error, result) {
        if (error) throw error;
        res.status(200).send(result);
    })

} else {
    interpretation.gzip = false;
    res.status(200).send(interpretation);
}

In Postman, the size of the response is the same, the content-type has changed, but there is no Content-Encoding header set in the response... 在Postman中,响应的大小相同,内容类型已更改,但响应中没有设置Content-Encoding标头...

在顶部(白色背景)是我的请求的标题。运行良好,标题由我的应用程序接收。响应标头错过了我的内容编码。

Look at the App Engine FAQ , specifically the answer to the question " How do I serve compressed content? ": 查看App Engine常见问题解答 ,特别是“ 如何提供压缩内容? ”这一问题的答案:

....To force gzipped content to be served, clients may supply 'gzip' as the value of both the Accept-Encoding and User-Agent request headers. ....为了强制提供gzip压缩内容,客户端可以提供'gzip'作为Accept-EncodingUser-Agent请求头的值。 Content will never be gzipped if no Accept-Encoding header is present... 如果不存在Accept-Encoding标头,内容将永远不会被gzip压缩...

Also, in this group post there's an example of how to send a request with Cloud Functions using the combination of Accept-Encoding , User-Agent : 此外,在此组帖子中有一个示例,说明如何使用Accept-EncodingUser-Agent的组合发送带有Cloud Functions的请求:

curl -v "https://us-central1-<project>.cloudfunctions.net/test" -H "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" -H "Accept-Encoding: gzip"

You can probably try to use zlib module to handle compression and set an appropriate encoding to the response: 您可以尝试使用zlib模块来处理压缩并为响应设置适当的编码:

exports.helloWorld = function helloWorld(req, res) {
  const zlib = require('zlib');

  // Obtain JSON stream from your source...

  res.status(200);

  res.set('Content-Type', 'text/plain');
  res.set('Content-Encoding', 'gzip');

  json.pipe(zlib.createGzip()).pipe(res);
};

Of course, it's required to first check whether client accepts gzip . 当然,首先需要检查客户端是否接受gzip Also, using zlib encoding can be expensive, and the results ought to be cached. 此外,使用zlib编码可能很昂贵,结果应该缓存。

Expressjs Production best practices: performance and reliability says below, which could be adapted for basic Node.js. Expressjs 生产最佳实践:性能和可靠性如下所述,可以适用于基本的Node.js. They use the popular compression package available on NPM . 他们使用NPM上流行的压缩包

Use gzip compression 使用gzip压缩

Gzip compressing can greatly decrease the size of the response body and hence increase the speed of a web app. Gzip压缩可以大大减小响应主体的大小,从而提高Web应用程序的速度。 Use the compression middleware for gzip compression in your Express app. 在Express应用程序中使用压缩中间件进行gzip压缩。 For example: 例如:

var compression = require('compression')
var express = require('express')
var app = express()
app.use(compression())

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

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