koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

Run middleware for a specific path + method #33

Closed Aurelsicoko closed 8 years ago

Aurelsicoko commented 8 years ago

Hi! Thanks for your amazing work on this router!

I was wondering, if the router is able to execute a middleware before hitting a specifc path + method. In the documentation, I saw that is possible for a specific path but without method allocated to it.

var router = require('koa-joi-router');
var users = router();

users.get('/user/:id', handler);
users.use('/:id', runThisBeforeHandlerForGET);

users.post('/user', handler);
users.use('/:id', runThisBeforeHandlerForPOST);

Is it possible?

Aurelsicoko commented 8 years ago

Okay, I found the solution. This is pretty simple.

var router = require('koa-joi-router');
var users = router();

router.route({
   method: 'GET',
   path: '/user/:id',
   handler: [runThisBeforeHandlerForGET, handler]
});

router.route({
   method: 'POST',
   path: '/user',
   handler: [runThisBeforeHandlerForPOST, handler]
});