Friday 5 May 2017

dynamicly add handlers to an express route function

what i am actually mean is, assume i have this code:

var ARouter = Router();    

@Validate({
        params: {
            id: joi.number(),
            x: joi.number()
        }
    })
    @HttpGet('/foo/:id')
    foo(req: Request, res: Response) {
        res.send('in foo').end();
    }

function HttpGet(path: string) {
    return function (target: ApiController, propertyName: string, descriptor: TypedPropertyDescriptor<RequestHandler>) {
        ARouter.get(path, descriptor.value);
    }
}

what i have here is a router, decorators, and a foo function. the HttpGet decorator creates a route with the path 'foo/:id' and foo as its only handler in ARouter.

i want the @validate decorator to add another handler (specific function middleware, will be called before foo) to the foo route handlers stack. e.g. like it was router.get('/foo/:id/, validationFunction, foo).

is there a way to dynamiclly add handler to the foo route in the router?

Thanks!



via Tamir Shasha

No comments:

Post a Comment