Node.js 如何使用npm压缩包压缩sails/express中的动态响应
在sails项目中使用npm compression包时,我观察到来自服务器的响应被gzip压缩,但我有一些API的响应没有被gzip压缩 根据文档app.use(compression())本身压缩所有响应,但我在API中找不到任何响应头作为“内容编码”:“gzip”在响应中包含JSON数据 以下是我在回复中收到的标题:Node.js 如何使用npm压缩包压缩sails/express中的动态响应,node.js,express,npm,compression,sails.js,Node.js,Express,Npm,Compression,Sails.js,在sails项目中使用npm compression包时,我观察到来自服务器的响应被gzip压缩,但我有一些API的响应没有被gzip压缩 根据文档app.use(compression())本身压缩所有响应,但我在API中找不到任何响应头作为“内容编码”:“gzip”在响应中包含JSON数据 以下是我在回复中收到的标题: access-control-allow-credentials: true access-control-allow-origin: https://xxxxxxxxxx.
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>
如果有人知道如何压缩这些响应,请帮助我,我在native express应用程序中也尝试了同样的方法,但面临着同样的问题。解决方案:我发现了这个问题,尝试了从应用程序级别进行压缩的可能方法,但没有任何解决方法。我使用Apache服务器作为中介,它非常适合压缩动态响应。
/**
* 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())
}
};