NestJS Module 2
Controllers, routing and versioning interview questions
Interview questions on controllers, routing and versioning: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
Which status code does a NestJS handler send by default, and how do you change it?
-
Fresher to mid
Why can @Get(':id') declared before @Get('me') break GET /users/me on the Express adapter?
Answered in the lesson Route order, paths and conflict checks
-
Fresher to mid
What does VERSION_NEUTRAL do in a NestJS app that uses versioning?
Answered in the lesson Versioning, global prefixes and the router module
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A NestJS handler injects @Res() to set a response header, then returns an object. In production these requests time out, though the handler finishes in milliseconds. Explain what happened, and give two ways to fix it.
- What it tests
- Understanding library-specific mode, what Nest stops doing when @Res() is injected, and choosing the fix that keeps Nest's own response handling.
- First step
- Notice that the handler finished but nothing was sent. Injecting @Res() switches the handler to library-specific mode: Nest ignores its return value and expects the handler to call res.json() or res.send() itself. Nobody did, so each client waited until a timeout.
- Follow-ups
-
- Which fix keeps interceptors and @HttpCode() working?
- How would you find every handler with the same bug?
- Why did no test catch it?
From the lesson Controllers and route handlers
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Fresher to senior
A paginated endpoint reads @Query('limit') limit: number and fetches limit + 1 rows to know whether a next page exists. With ?limit=10 it asks the database for 101 rows. Explain the bug, and fix it so that the codebase cannot repeat it.
- What it tests
- Knowing that query values arrive as strings whatever their TypeScript type, and moving conversion and validation into one place that every handler uses.
- First step
- Check the value at run time, not its type annotation. Query values arrive as strings, so limit is '10', and '10' + 1 joins text into '101'. The annotation number is erased at compile time and converts nothing.
- Follow-ups
-
- Where should the conversion live, and why not in each handler?
- What should ?limit=abc or ?limit=-5 return?
- How would a test have caught this?
From the lesson Controllers and route handlers
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
You are switching on Nest 12's routeConflictPolicy for a large app on the Express adapter. Which settings do you choose for development, CI and production, and how do you make sure a lost route fails the build?
- What it tests
- Telling duplicates from overlap, knowing what each severity does at start-up, and pairing the checks with tests that prove each route is reachable.
- First step
- Separate the two kinds of conflict. A duplicate means two identical routes, so one handler never runs. A shadow means two patterns can match the same request, which may be deliberate. Then decide, for each kind and each environment, whether off, warn or error fits.
- Follow-ups
-
- Would you set shadow to 'error' in CI?
- Where would you put the options so that tests and production agree?
- What can the checks never find?
From the lesson Route order, paths and conflict checks
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
A team sets routeResolutionStrategy to 'specificity' together with routeConflictPolicy { shadow: 'error' } in CI. Predict what happens to a controller that declares 'me' before ':id', and to one that declares ':id' first, and explain the rule behind your prediction.
- What it tests
- Reasoning about what each option does and how Nest combines them, instead of assuming that two safety options add up to more safety.
- First step
- Start from each option alone. Specificity changes the order Nest registers routes in. The shadow check compares pairs of routes that can match the same request. Then ask which pairs the check still reports once the routes have been sorted.
- Follow-ups
-
- Is the outcome a bug in Nest?
- What would you change in the team's settings?
- How would you prove the controllers work, whatever the settings say?
From the lesson Route order, paths and conflict checks
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
You are upgrading a Nest 10 app on Express 4 to Nest 12. Its routes include 'files/*', 'orders/:id?' and a path with regular-expression characters. What changes, what stops working, and how do you make the migration safe?
- What it tests
- Knowing path-to-regexp 8's syntax and how Nest 12 treats old patterns, and planning a migration that finds every pattern before production does.
- First step
- List every path string in the app, in route decorators, middleware forRoutes() calls and global prefix exclusions, and check each one against the path syntax of Express 5, which Nest 12 uses for all three. Then decide how you will find the patterns the list missed before production does.
- Follow-ups
-
- What does the handler of a wildcard route receive now?
- How do you keep /files working as well as /files/report.pdf?
- How do you find patterns the code search missed?
From the lesson Route order, paths and conflict checks
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
Older mobile app versions never send a version header. Right after a small configuration change to a NestJS app that uses header versioning, those apps start getting 404 from endpoints that worked before. Explain how a configuration change can cause this, how you would find it, and how you fix it without breaking newer clients.
- What it tests
- Knowing what each versioning option really does to controllers and requests, reproducing the failure, and choosing declarations that serve clients without a version on purpose.
- First step
- Reproduce one request without the header against the configuration before and after the change, and compare which controllers answer. Then list, for each controller, which versions it declares, and what the versioning options give the controllers that declare none.
- Follow-ups
-
- Which declaration serves old clients and still rejects unknown versions?
- Why can the documentation and a run disagree here, and which do you trust?
- What tests stop this from happening again?
From the lesson Versioning, global prefixes and the router module
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A monitoring check of GET /health against a NestJS API on Express gets a 404 whose body the monitor cannot parse, although every other error of the API is JSON. The app calls setGlobalPrefix('api'). Explain what answered, and how you would make every response of the API JSON.
- What it tests
- Knowing where Nest's not-found handling lives relative to a global prefix, how adapters differ, and which fix suits a public API.
- First step
- Find out which code produced the response. A path outside the prefix may never reach any of Nest's handlers, so compare a request inside the prefix that matches no route with one outside it, and look at the content type of each answer.
- Follow-ups
-
- Why does the Fastify adapter behave differently?
- Should health checks live under the prefix or outside it?
- How do you keep a fix from breaking when the prefix changes?
From the lesson Versioning, global prefixes and the router module
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
Design how a public NestJS API introduces version 2 of its menu endpoint, keeps existing clients working, and retires version 1. Cover the versioning type, the controller declarations, and how clients learn about the retirement.
- What it tests
- Choosing a versioning type for the clients, declaring versions so that old and new clients both work, and retiring a version with standard signals and evidence instead of a guess.
- First step
- Start from the clients: who calls the API, whether they can send headers, and whether URLs are cached or shared. That decides between URI and header versioning. Then decide what a request without any version should get, before writing a single declaration.
- Follow-ups
-
- What does a request without a version get, and why?
- How do clients learn that version 1 is going away?
- When is it safe to remove the version 1 controller?
From the lesson Versioning, global prefixes and the router module
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes