【问题标题】:Is it possible to define iron-router server side routes that respond to certain HTTP actions?是否可以定义响应某些 HTTP 操作的 Iron-router 服务器端路由?
【发布时间】:2023-03-30 08:40:01
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

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

    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(),但我不确定。

    【讨论】:

    • 听起来目前还没有内置支持,所以像本例中那样手动滚动是最好的解决方案。
    猜你喜欢
    • 2013-09-03
    • 2015-02-27
    • 1970-01-01
    • 2013-12-09
    • 2014-02-13
    • 2015-03-14
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多