Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would say that it's hard to solve this in a clean manner using express-resource, since it doesn't allow for route-specific middleware (at least not in a clean way).</p> <p>I would opt for a similar layout as an express-resource module, but route it with plain old express. Something like this:</p> <pre><code>// Resource var forum = { index: // ... show: // ... create: // ... update: // ... destroy: // ... }; // Middleware var requireRole = function(role) { return function(req, res, next) { if('user' in req.session &amp;&amp; req.session.user.role === role) next(); else res.send(403); } }; // Routing app.get('/forums', forum.index); app.get('/forums/:id', forum.show); app.post('/forums', requireRole('moderator'), forum.create); // Only moderators can create forums app.delete('/forums/:id', requireRole('admin'), forum.destroy); // Only admins can delete forums </code></pre> <p><strong>UPDATE:</strong> There have been ongoing discussions regarding route-specific middleware in express-resource, e.g. <a href="https://github.com/visionmedia/express-resource/pull/45">here</a>. The prevailing view seems to be to have an array per action, e.g.:</p> <pre><code>var forums = { index: [ requireRole('foo'), function(req, res, next) { ... } ] }; </code></pre> <p>You could take a look through the pull requests and see if there is anything you could use. I totally understand it, of course, if you don't feel comfortable with that. I'm pretty sure we will see something like this in express-resource in the future.</p> <p>The only other solution I can think of is along the lines of Jan Jongboom's answer, which would be to mount the resources with express-resource, but have middleware attached "outside" of that, something like:</p> <pre><code>app.delete('*', requireRole('admin')); // Only admins are allowed to delete anything app.put('/forums/*', requireRole('moderator')); // Only moderators are allowed to update forums </code></pre> <p>But I regret that this leaks URLs all over the place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload