避免在服务器端路由上使用全局 onBeforeAction 方法

Avoiding global onBeforeAction methods on a server side route

本文关键字:全局 onBeforeAction 方法 服务器端 路由      更新时间:2023-09-26

我有一个用于Facebook共享的服务器端路由(因为它现在需要一个URL)

this.route('share',{
    path: '/share',
    where: 'server'
}).get(function(data){
    var query = data.query;
    this.response.write('<html>');
    this.response.write('<head>');
    // content type is text/html by default
    this.response.write('<title></title>');
    if(query){
        for(var prop in query){
            if(query.hasOwnProperty(prop)){
                this.response.write('<meta property="'+prop+'" content="'+query[prop]+'" />');
            }
        }
    }
    this.response.write('</head>');
    this.response.write('<body></body>');
    this.response.write('</html>');
    this.response.end();
});

但是我的全局Router.onBeforeAction()函数仍然被调用,它使用jQuery(动画) - 这会导致服务器端出现问题。

有没有办法跳过全局钩子,或者我应该只检查是否定义了 jQuery?

根据 iron:router 指南,你应该使用 except 选项声明你的全局 onBeforeAction 钩子:

Router.onBeforeAction(yourGlobalHook, {
  except: ['share']
});