Reputation: 412
I want to redirect user to another url in my server, but I do not want to hardcode url like res.redirect('/hello_world')
. Instead I want to just specify handler's url of specified controller like res.redirect(HelloWorldController.handlerName.url)
where HelloWorldContoller is
@Controller()
export class HelloWorldController {
@Get('hello_world')
handlerName(): string {
return 'Hello World!';
}
}
Upvotes: 7
Views: 15643
Reputation: 4788
For example, you can use Reflect to get metadata like this:
import { PATH_METADATA } from '@nestjs/common/constants';
@Controller('api')
export class ApiController {
@Get('hello')
root() {
let routePath = Reflect.getMetadata(PATH_METADATA, StaticController);
routePath += '/' + Reflect.getMetadata(PATH_METADATA, StaticController.prototype.serveStatic);
console.log(routePath); will return `api/hello`
return {
message: 'Hello World!',
};
}
}
Why it returns api/hello if I need a path not of self url, but other controller's url?
Here StaticController
is used as an example, You can import any other controllers and pass it to Reflect.getMetadata(PATH_METADATA, AnyOtherController);
Upvotes: 8