Run ❯
Get your
own Node
server
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
const ROUTES: any[] = []; function Controller(prefix: string = '') { return function (constructor: Function) { (constructor as any).prototype.prefix = prefix; }; } function Get(path: string = '') { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { ROUTES.push({ method: 'get', path, handler: descriptor.value, target: target.constructor }); }; } @Controller('/users') class UserController { @Get('/') getAllUsers() { return { users: [{ id: 1, name: 'John' }] }; } @Get('/:id') getUserById(id: string) { return { id, name: 'John' }; } } function registerRoutes() { ROUTES.forEach(route => { const prefix = (route.target.prototype as any).prefix || ''; console.log(`Registered ${route.method.toUpperCase()} ${prefix}${route.path}`); }); } registerRoutes();
Expected console output:
Registered GET /users Registered GET /users/:id