Express.js Route Handler Registration (typescript)
JS
S
JavaScriptRoute handler registration function
1function Get(param?: string): any {
2 param = param || '/';
3 return (target: any, propertyKey: string): TypedPropertyDescriptor<any> => {
4 const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey);
5 const originalMethod = descriptor.value;
6 target._routes = target._routes || [];
7
8 function asyncMiddleware (request: Request, response: Response, next: NextFunction) {
9 const prom = originalMethod(request, response, next)
10 if (prom && prom.then) {
11 prom.then((result: any) => response.status(200).send(result)).catch((err: Error) => next(err))
12 }
13 }
14 target._routes.push({url: param, method: 'get', handler: asyncMiddleware});
15 return descriptor;
16 };
17}Created on 11/13/2019