检查每个页面请求上的标题

时间:2016-09-30 06:44:30

标签: apostrophe-cms

我需要检查每个页面请求的请求标头。我认为尝试这个的地方将是'撇号 - 快递'模块。我创建了自己的模块'auth',扩展了'撇号 - 快递':

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

但这不起作用。我收到错误'撇号 - 模板\ index.js“597 req.browserCall('apos.pageReadywhenCalm($(”body“));')'

您能否告诉我应该在每个页面请求中挂钩以运行某些自定义检查/规则(例如,如果某个cookie不存在,那么将用户重定向到其他站点)?

谢谢!

1 个答案:

答案 0 :(得分:3)

我是P'unk Avenue的Apostrophe的主要设计师。

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

拦截每个Express请求

相反,只需利用middleware模块的apostrophe-express选项即可。您可以在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文件中,以“隐式子类化”该模块。这为您提供自己的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   
  };
};

在页面呈现之前拦截请求

您指定了“每个网页请求”,我将其解释为“每个网页请求”。但是你有可能只想要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);
    };
  }
};

对于具有这种方法的每个模块,撇号总是调用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设置为与URL匹配的最长“路径前缀”的页面。例如,如果URL为/ foo / bar且/ foo存在但/ foo / bar不存在,则req.data.bestPage将为/foo。请注意,这意味着req.data.bestPage将成为最糟糕情况下的主页。

请参阅apostrophe-custom-pages模块,了解您可以使用此功能完成的事情。

希望这有用!

相关问题