是否可以定义响应某些HTTP动作的Iron-Router服务器端路由? [英] Is it possible to define iron-router server side routes that respond to certain HTTP actions?

查看:44
本文介绍了是否可以定义响应某些HTTP动作的Iron-Router服务器端路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a basic server side route defined in iron-router like:

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

这似乎是通过任何HTTP操作来响应"/foo"处的请求,即GET到"/foo"和POST到"/foo"都触发了该路由.

This appears to respond to a request at "/foo" with any HTTP action, i.e. a GET to "/foo" and a POST to "/foo" both trigger this route.

  1. 是否可以将响应限制为GET操作,并允许 其他动作未找到?
  2. 类似地,是否有可能获得GET 到通过一个路由处理的"/foo",而通过POST到由另一路由处理的"/foo"?
  1. Is it possible to limit the response to a GET action, and let the other actions be notFound?
  2. Similarly, is it possible to have a GET to "/foo" handled by one route, and a POST to "/foo" handled by another?

推荐答案

您绝对可以检查该方法,并且仅在需要时才响应,例如:

You can definitely check the method and only respond if it's the one you want, e.g., like this:

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

The second question beats me. It might be possible to use this.next() somehow, but I'm not sure.

这篇关于是否可以定义响应某些HTTP动作的Iron-Router服务器端路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆