nestjs

How do I get the domain originating the request (of the front-end) in NestJS


My question is similar to the following: How do I get the domain originating the request in express.js? but I'm using NestJS. I found that there might be a good answer for express.js, but I cannot apply it on NestJS (req.origin is undefined).

Could anyone help me with this? Thank you in advance.


Solution

  • I found the answer for my question, we can use @Host('origin'). The value exists only when we call the back-end server from a real front-end, not from Postman or directly call back-end url from browser. Thank you Moazzam Arif for helping, allow use your example to show my answer below.

    @Controller('cats')
    export class CatsController {
      @Get()
      findAll(@Headers('origin') origin: string): string {
        return 'This action returns all cats';
      }
    }