如何使用npm压缩包压缩帆/快递中的动态响应

时间:2018-11-28 11:00:39

标签: node.js express npm compression sails.js

虽然在sails项目中使用 npm压缩包,但我发现来自服务器的响应被压缩,但是我有一些API的响应没有被压缩/压缩。

根据文档 app.use(compression())本身会压缩所有响应,但是我在包含JSON的API中找不到任何响应标头为“ content-encoding”:“ gzip”数据响应。

以下是我在响应中收到的标头:

access-control-allow-credentials: true
access-control-allow-origin: https://xxxxxxxxxx.com
access-control-expose-headers: 
content-length: 312
content-type: application/json; charset=utf-8
date: Wed, 28 Nov 2018 10:44:56 GMT
etag: W/"138-SIGn/Sj6rEAdXMhpUah0Xw"
status: 200
vary: X-HTTP-Method-Override, Accept-Encoding
x-powered-by: Sails <sailsjs.org>

此处内容编码:'gzip'丢失。

以下是我在config / http.js文件中使用的自定义中间件

/**
 * HTTP Server Settings
 * (sails.config.http)
 *
 * Configuration for the underlying HTTP server in Sails.
 * Only applies to HTTP requests (not WebSockets)
 *
 * For more information on configuration, check out:
 * http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.http.html
 */
var compression = require('compression')
module.exports.http = {

  /****************************************************************************
  *                                                                           *
  * Express middleware to use for every Sails request. To add custom          *
  * middleware to the mix, add a function to the middleware config object and *
  * add its key to the "order" array. The $custom key is reserved for         *
  * backwards-compatibility with Sails v0.9.x apps that use the               *
  * `customMiddleware` config option.                                         *
  *                                                                           *
  ****************************************************************************/
  customMiddleware : function(app){
    app.use(compression())
  }
};

如果有人知道如何压缩这样的响应,请帮我,我在本机Express应用程序中也尝试了同样的事情,但是面临相同的问题。

0 个答案:

没有答案