sails.js: 如何禁用 etag

sails.js: how to disable etags

我读到这篇关于在 sails.js 中禁用某些功能的文章 post 并不是很新。具体来说,我想尝试的是禁用 etags。

有谁知道如何在 sails.js (0.11.0) 中禁用它?

您可以在 the bootstap.js file 中禁用它:

// config/bootstrap.js
module.exports.bootstrap = function(cb) {

  // Add this line to access to the Express app and disable etags
  sails.hooks.http.app.disable('etag');

  // It's very important to trigger this callback method when you are finished
  // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)
  cb();
};

以上答案仅适用于动态文件。对于静态文件,您必须通过将此代码添加到 config/https.js 文件

来覆盖 www 中间件
www: (function() {
  var flatFileMiddleware = require('serve-static')('.tmp/public', {
    maxAge: 31557600000,
    etag: false
  });

  return flatFileMiddleware;
})(),

如果您仍想设置 cache-control header 年龄,则必须设置 maxAge。另请注意,这将覆盖用于定义该年龄的同一 http.js 文件中的缓存选项。

出于我的目的,我只想从 /min 文件夹中提供的静态文件中删除 etags,这些文件是缩小的 css 和 js 文件。所以要做到这一点,我必须在 routes.js 文件

中定义一条路线
'get /min/*': { 
  skipAssets: false,
  fn: [
    require('serve-static')('.tmp/public', {
      maxAge: process.env.NODE_ENV !== 'production' ? 0 : 31557600000,
      etag: false
    }),
  ]
}

如果您想了解更多详细信息,请参阅此处 https://github.com/balderdashy/sails/issues/5306#issuecomment-508239510