Saturday 6 May 2017

Whould you say it is a good idea to wrapp method with @HttpGet (express)

in short, this is the code:

var BRouter = Router();
class BController implements ApiController {

    public get router() { return BRouter; }
    public set router(router: Router) { BRouter = router; }

    @HttpGet('/foo')
    private foo(req: Request, res: Response, next: NextFunction) {
        res.end();
    }
}

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

I like this way because it attaches foo route path to foo (the other way is to init all class routes in a single function away from foo) But i can lose some express router functionality and maybe it wont work it further express versions..

What do you think? It is a good idea? There is a third way?

Thanks!



via Tamir Shasha

No comments:

Post a Comment