9

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.

1
  • How do you try to access request? Commented Mar 30, 2020 at 13:36

6 Answers 6

8

In my project below code is worked.

async login (@Headers() headers) {
  console.log('AUTHH LOGG', headers.host)
}

Sign up to request clarification or add additional context in comments.

Comments

5

If running @Headers() like this...

import { Get, Request, Headers } from '@nestjs/common';

@Get()
findAll(@Headers() headers: Headers) {
  console.log(headers);
}

it gives you

{
  "host": "localhost:3001",
  "connection": "keep-alive",
  "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\"",
  "accept": "application/json, text/plain, */*",
  "sec-ch-ua-mobile": "?0",
  "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) OtherShtuff",
  "sec-ch-ua-platform": "\"Windows\"",
  "origin": "http://localhost:3000",
  "sec-fetch-site": "same-site",
  "sec-fetch-mode": "cors",
  "sec-fetch-dest": "empty",
  "referer": "http://localhost:3000/",
  "accept-encoding": "gzip, deflate, br",
  "accept-language": "en-US,en;q=0.9,fr;q=0.8,es;q=0.7,de;q=0.6",
  "cookie": "_ga=GA1blahblahblah"
}

so to get the actual host origin as asked, using the below works. However, using @Headers() headers: Header then calling headers.origin causes typescript errors since Headers doesn't define all the parameters above. So you'd have to define it yourself, so I would stick to one of the two options below.

import { Get, Headers } from '@nestjs/common';

@Get()
findAgain(@Headers('origin') origin: string) {
  console.log(origin);
}

// or 

@Get()
findEvenMore(@Request() req: Request) {
  console.log(req.get("origin"));
  console.log(req.headers.origin);
}

Comments

2

from docs, you can have access to request object in Nestjs.

Nest provides access to the request object of the underlying platform (Express by default). We can access the request object by instructing Nest to inject it by adding the @Req() decorator to the handler's signature.

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

Comments

1

For anyone looking for the right method in 2025 like me (Using Nestjs 11 +)

This works well for me for getting any header. Use the @Req decorator and access the headers dictionary.

 async returnOrigin(@Req() req: Request) {
    const origin = req.headers['origin'];
    return origin;
  }

Also, if you're looking to access a familiar flavor of control schema instead of using Nest's abstraction, Nest is just using Express under the hood so you can always assign req the Express request type and access all the same methods. You can use it in this case with req.get('someheader')

import { Request as ExpressRequest } from 'express';

@Controller()
export class AppController {
  constructor() {}

 @Get()
 async returnOrigin(@Req() req: ExpressRequest) {
    const origin = req.get('origin');
    return origin;
  }
}

Comments

0

In the controller use the @Req() decorator to access the request object and req.get('origin') will give you the domain from which the request originated

    @Get()
    async testAPIKey(@Req() req, @Headers() headers) {
       const origin = req.get('origin');
       console.log(origin) // This will return the origin.
    }

Comments

0

It depends on where you want to have it. Besides from the all answers bellow, you can also extract from Request object in custom guard/interceptor by:

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';

@Injectable()
export class Your_Custom_Guard_Name_Class implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    // Headers object contains all what you need
    const { host } = context.switchToHttp().getRequest().headers; 
      // Your code
    } 
  }
}

And use it in your controller like:

@UseGuards(Your_Custom_Guard_Name_Class)
@Post('endpointWithCustomGuard')
async login() {
  ...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.