2014-02-25 99 views
0

使用铁路由器有可能增加挂钩,像这样:流星铁路由器的服务器端挂钩

// this hook will run on almost all routes 
Router.before(mustBeSignedIn, {except: ['login', 'signup', 'forgotPassword']}); 

// this hook will only run on certain routes 
Router.before(mustBeAdmin, {only: ['adminDashboard', 'adminUsers', 'adminUsersEdit']}); 

参见:https://github.com/EventedMind/iron-router#using-hooks

但本细则并没有说如何让这些挂钩“服务器端” 。

这个想法是创建一个钩子,它将监督所有路径的发布集合,除了一个或两个特定的路线,我希望更多地控制发布的内容。

回答

4

Iron Router在客户端和服务器上都是一样的,声明可以在客户端和服务器都可用的目录/文件上完成。

默认情况下,声明的路由是针对客户端的。如果你想要一个路由作为服务器端,那么你通过包含where: 'server'来明确声明。

official docs摘自:

定义路线和配置路由器是服务器和客户端上几乎相同。默认情况下,路由创建为客户端路由。您可以指定的路线是用于服务器通过像这样的路线提供了一个地方性质:

Router.map(function() { 
    this.route('serverRoute', { 
    where: 'server', 

    action: function() { 
     // some special server side properties are available here 
    } 
    }); 
}); 

注意,必须在要放在Router.map,而不是在控制器上。

服务器操作函数(RouteControllers)具有不同的属性和方法。也就是说,服务器上还没有渲染。所以渲染方法不可用。另外,您不能等待订阅或在服务器上调用wait方法。服务器路由就像在客户端一样获得Connect请求的裸请求,响应和下一个属性,以及params对象。

Router.map(function() { 
    this.route('serverFile', { 
    where: 'server', 
    path: '/files/:filename', 

    action: function() { 
     var filename = this.params.filename; 

     this.response.writeHead(200, {'Content-Type': 'text/html'}); 
     this.response.end('hello from server'); 
    } 
    }); 
}); 

正如你看到的,只有一个命名约定,因此,你能说出这样的事情:

Router.before(someFilter, {only: ['clientRoute1', 'clientRoute2', 'serverRoute1']}); 

Router.before(someOtherFilter, {except: ['clientRoute3', 'clientRoute4', 'serverRoute2']}); 

就像你通常会。