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