简体   繁体   English

流星铁路由器服务器端挂钩

[英]Meteor Iron Router server-side hooks

Using Iron router it's possible to add hooks like so: 使用Iron路由器,可以像这样添加钩子:

// 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']});

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

But the documentations does not say how to make these hooks "server side". 但是文档没有说明如何使这些挂钩成为“服务器端”。

The idea is to create a hook which will oversee publishing Collections for all routes, except for one or two specific routes where I want more control over what's published. 这个想法是创建一个钩子,该钩子将监督所有路线的发布集合,但我希望对发布内容进行更多控制的一条或两条特定路线除外。

Iron Router is just the same both on client and server and the declarations can be done on a directory/file which is available to both client and the server. Iron Router在客户端和服务器上都是相同的,并且声明可以在客户端和服务器都可用的目录/文件中完成。

By default, the declared routes are for the client. 默认情况下,声明的路由用于客户端。 If you want a route to be server side, then you explicitly declare so by including where: 'server' . 如果要使路由成为服务器端,则可以通过包含以下内容来明确声明where: 'server'

Taken from the official docs : 取自官方文档

Defining routes and configuring the Router is almost identical on the server and the client. 在服务器和客户端上,定义路由和配置路由器几乎相同。 By default, routes are created as client routes. 默认情况下,路由被创建为客户端路由。 You can specify that a route is intended for the server by providing a where property to the route like this: 您可以通过为路由提供一个where属性来指定该路由用于服务器:

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

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

Note that where must be placed in Router.map, not on the controller. 请注意,必须将其放置在Router.map中,而不是控制器上。

Server action functions (RouteControllers) have different properties and methods available. 服务器操作功能(RouteControllers)具有可用的不同属性和方法。 Namely, there is no rendering on the server yet. 即,服务器上尚无渲染。 So the render method is not available. 因此render方法不可用。 Also, you cannot waitOn subscriptions or call the wait method on the server. 另外,您不能使用waitOn订阅或在服务器上调用wait方法。 Server routes get the bare request, response, and next properties of the Connect request, as well as the params object just like in the client. 服务器路由获得连接请求的裸请求,响应和下一个属性,以及客户端中的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');
    }
  });
});

As you see, there is only one naming convention, therefore you can state something like this: 如您所见,只有一种命名约定,因此您可以这样声明:

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

or 要么

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

just like you normally would. 就像平时一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM