Questions
19 of 24
1How does NestJS resolve constructor injection at runtime?
2What does @Optional() decorator do in NestJS?
3What is the difference between moduleRef.get() and moduleRef.resolve() in NestJS?
4When do you need @Inject(TOKEN) vs plain constructor injection in NestJS?
5What is ModuleRef in NestJS and when would you use it?
6What are the three provider scopes in NestJS and when is each appropriate?
7What is the correct architectural solution for circular dependencies beyond forwardRef()?
8How do you detect and debug circular dependency errors in large NestJS projects?
9What is property-based injection in NestJS and when should you use it?
10How do you write a unit test for a NestJS service that has injected dependencies?
11What is a DI token and what types can be used as tokens in NestJS?
12How do you pass a custom REQUEST object to a REQUEST-scoped provider for queues or CRON jobs in NestJS?
13How does forwardRef() solve circular dependencies in NestJS and how does it work internally?
14How do you implement the Strategy pattern using NestJS DI?
15How do you replace a provider in a NestJS test module using overrideProvider()?
16What is Inversion of Control (IoC) and how does NestJS implement it?
17What is the difference between Dependency Injection (DI) and Inversion of Control (IoC)?
18What is scope propagation in NestJS and why can it impact performance?
19How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?
20How does moduleRef.resolve() work with scoped providers and what is ContextIdFactory used for?
21What role does reflect-metadata play in NestJS DI?
22What is a circular dependency in NestJS and how does it manifest at runtime?
23What is LazyModuleLoader in NestJS and how does it differ from standard DI?
24What happens to REQUEST-scoped providers during WebSocket connections or microservice message handling in NestJS?
19 / 24

How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?

Difficulty: 5/10
Request-scoped providers, Dependency injection, NestJS request lifecycle

Use the REQUEST token exported from @nestjs/core along with @Inject(). The provider must be decorated with scope: Scope.REQUEST. For GraphQL contexts use the CONTEXT token from @nestjs/graphql instead.

Injecting the raw HTTP request
Context tokens by transport:
  1. 1

    HTTP — use REQUEST token from @nestjs/core; resolves to the Express or Fastify request object.

  2. 2

    GraphQL — use CONTEXT token from @nestjs/graphql; resolves to the GraphQL execution context.

  3. 3

    Microservices — REQUEST token resolves to the incoming message payload object.

  4. 4

    WebSockets — REQUEST token resolves to the Socket client instance.

Scenario Questions

0-2 years experience

  1. 1You need to log the incoming IP address inside a service that's request-scoped. How would you get the raw request object into that service?
  2. 2If you accidentally mark the provider as singleton, what will happen when you try to access request-specific data?

2-5 years experience

  1. 1We're adding a feature that reads a custom header and stores it in a per-request cache service. Walk me through how you'd inject the request object into that cache service and any pitfalls.
  2. 2During testing, the request object is undefined in a request-scoped provider. What could cause that and how would you debug it?

5-8 years experience

  1. 1Our API gateway creates thousands of concurrent requests. Discuss the trade-offs of using request-scoped providers with injected request objects versus passing data via method parameters.
  2. 2You notice memory usage growing over time when using request-scoped providers. How would you investigate if the injected request object is causing a leak?

8+ years experience

  1. 1We plan to migrate from NestJS's default Express platform to Fastify. How would that affect the way you inject the raw request object into request-scoped providers, and what architectural changes would you recommend?
  2. 2Multiple teams need to share request metadata across different modules without tightly coupling to the HTTP layer. Propose a design that leverages request-scoped injection while keeping the core domain services framework-agnostic.

Follow-up Questions

  • What would you do if you needed the request in a provider that is currently singleton?
  • Can you describe how Nest resolves the REQUEST token under the hood?
  • How would you test a request-scoped provider that depends on the raw request?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.