2

我在 Iron-router 中定义了一个基本的服务器端路由,例如:

this.route('foo', {
  where: 'server',
  path: '/foo',
  action: function() {
    // handle response
  }
});

这似乎使用任何 HTTP 操作响应“/foo”处的请求,即对“/foo”的 GET 和对“/foo”的 POST 都触发此路由。

  1. 是否可以限制对 GET 操作的响应,并让其他操作找不到?
  2. 类似地,是否有可能让一个路由处理“/foo”的 GET,而另一个路由处理“/foo”的 POST?
4

1 回答 1

3

您绝对可以检查该方法,并且仅在您想要的方法时才响应,例如,像这样:

Router.map(function () {
    this.route('route', {
        path: '/mypath',
        where: 'server',
        action: function() {
            if (this.request.method != 'GET') {
                // do whatever
            } else {
                this.response.writeHead(404);
            }
        }
    })
});

第二个问题打败了我。可能以this.next()某种方式使用,但我不确定。

于 2014-01-06T05:07:55.287 回答