Is it possible to define iron-router server side routes that respond to certain HTTP actions?

十年热恋 提交于 2019-12-02 02:48:18

问题


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

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

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. 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?

回答1:


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);
            }
        }
    })
});

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



来源:https://stackoverflow.com/questions/20943084/is-it-possible-to-define-iron-router-server-side-routes-that-respond-to-certain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!