是否可以定义响应某些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.
- 是否可以将响应限制为GET操作,并允许 其他动作未找到?
- 类似地,是否有可能获得GET 到通过一个路由处理的"/foo",而通过POST到由另一路由处理的"/foo"?
- Is it possible to limit the response to a GET action, and let the other actions be notFound?
- 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.