I am trying to register methods and functions into express. For example:
@Controller("/api/users/")
export class UserController {
constructor() {
console.log("constructor")
}
@GET("get/:id")
public getUser(req: Request, res: Response) {
}
}
The decorator is as follows:
export function Controller(basePath: string) {
return function(target: any) {
const instance = new target();
const methodList = Object.getOwnPropertyNames(target.prototype).map(m => target.prototype[m])
methodList.forEach(method => {
console.log(method) //<--- What should I put here?
})
}
}
export function GET(url: string) {
return function (target: any, propertyKey: String, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
descriptor.value.url = url
}
return descriptor;
}
}
How do I get the value “get/:id” without invoking getUser function?
The goal is to reference getUser
to Express router. So in this case, I want to register getUser(req, res)
as express.router.get(basePath + url, method)
I don’t know what to put there to replace console.log(method)
to register the methods