在每个页面请求上检查 headers

Check headers on each page request

我需要检查每个页面请求的请求 headers。我认为尝试这个的地方是在 'apostrophe-express' 模块中。我创建了自己的模块 'auth' 扩展了 'apostrophe-express':

module.exports = {
extend: 'apostrophe-express',
name: 'auth',
alias: 'auth',
absoluteUrl: function(req, res, next){
    console.log(req);
 }
};

但这不起作用。我收到错误消息 'apostrophe-templates\index.js" 597 req.browserCall('apos.pageReadywhenCalm($("body"));')'

你能告诉我应该在每个页面请求中挂钩到 运行 一些自定义 checking/rule 的位置吗(例如,如果某个 cookie 不存在,则将用户重定向到不同的站点)?

谢谢!

我是 P'unk Avenue 的 Apostrophe 的首席建筑师。

您不想以新名称扩展 apostrophe-express 模块。该模块提供 apos.app 单例,因此以新名称扩展它只会设置 apos.app 两次,从而导致混淆和潜在问题。

拦截每个 Express 请求

相反,只需利用 apostrophe-express 模块的 middleware 选项。您可以在 app.js:

var apos = require('apostrophe')({
  modules: {
    // Other module configuration here, then ...
    'apostrophe-express': {
      middleware: [
        function(req, res, next) {
          // Do whatever you like here
          console.log(req.url);
          // Let the request continue. You could also
          // use res.redirect, res.send, etc. and
          // bypass Apostrophe, in which case you
          // should NOT call next
          return next();
        }
      ]
    }
  }
});

这就是您所需要的,您可以使用 require 从另一个文件中提取中间件功能,从而减少 app.js 中的混乱。然而,值得指出的是,您可以将此代码移动到项目中的 lib/modules/apostrophe-express/index.js 文件,移动到 "implicitly subclass" 该模块。这也打开了提供您自己的 construct 属性 的大门,如果您愿意,这将允许您覆盖模块的任何方法。

如果你这样处理,你根本不需要触碰 app.js:

// in lib/modules/apostrophe-express/index.js at project level
module.exports = {
  middleware: [
    function(req, res, next) {
      console.log(req.url);
      return next();
    }
  ],
  construct: function(self, options) {
    // If you want, override methods of the module here   
  };
};

在页面呈现之前拦截请求

您指定了 "each page request,",我将其解释为 "each web request.",但您可能特别只希望请求 Apostrophe 即将构建并发送适当的网页。

为此,只需将 pageBeforeSend 方法添加到您自己的任何模块即可。假设我们的模块名为 cool-stuff:

// in app.js

var apos = require('apostrophe')({
  modules: {
    // Other module configuration here, then ...
   'cool-stuff': {}
  }
});


// In lib/modules/cool-stuff/index.js

module.exports = {
  construct: function(self, options) {
    self.pageBeforeSend = function(req, callback) {
      // Careful, there isn't always a page object; we could be
      // rendering /login for instance
      console.log(req.data.page && req.data.page._url);
      return callback(null);
    };
  }
};

Apostrophe 总是为每个具有此类方法的模块调用 pageBeforeSend

如上所示,注意不要假定 req.data.page 已设置,因为在某些情况下,Apostrophe 会呈现完整的网页作为响应,但 Apostrophe 的页面树中没有相应的页面对象。

加载页面对象后立即拦截

还有一个选择:如果 pageBeforeSend 对您来说太晚了,例如因为您希望小部件加载程序看到您对 req 所做的更改,请使用 pageServe 而不是...

// in app.js

var apos = require('apostrophe')({
  modules: {
    // Other module configuration here, then ...
   'cool-stuff': {}
  }
});

// lib/modules/cool-stuff/index.js

module.exports = {
  construct: function(self, options) {
    self.pageServe = function(req, callback) {
      // Express request URL
      console.log(req.url);
      // URL of the best matching page available
      console.log((req.data.page || req.data.bestPage)._url);
      return callback(null);
    };
  }
};

请注意,我允许 req.data.pagereq.data.bestPage 存在。如果 URL 与页面不完全匹配,Apostrophe 会将 req.data.bestPage 设置为最长 "path prefix" 匹配 URL 的页面。例如,如果 URL 是 /foo/bar 并且 /foo 存在但 /foo/bar 不存在,则 req.data.bestPage 将是 /foo。请注意,这意味着 req.data.bestPage 在最坏的情况下将成为主页。

请参阅 apostrophe-custom-pages 模块,了解您可以用它做的巧妙事情。

希望对您有所帮助!