Questions
20 of 25
1How do you build a reusable pagination and sorting query DTO that can be extended by feature-specific DTOs in NestJS?
2How do you use ClassSerializerInterceptor with @Exclude() and @Expose() to control response shape in NestJS?
3What versioning strategies does NestJS support and how do you enable versioning?
4How do you implement idempotency keys for POST requests to prevent duplicate operations in NestJS?
5How do you implement API rate limiting per user or per IP in NestJS?
6How do you handle raw body access for webhook signature verification in NestJS?
7How do you stream a large file or dataset as a response without loading it fully into memory in NestJS?
8How do you implement content negotiation so an endpoint returns JSON or CSV based on the Accept header in NestJS?
9How do you set a default version so unversioned requests are handled by a specific version in NestJS?
10How do you document DTO properties for Swagger and handle optional vs required fields in NestJS?
11How do you handle HATEOAS hypermedia links in NestJS REST responses?
12What decorators does NestJS provide for route parameters and how do they differ from query params?
13How do you handle a route where the param can be either a numeric ID or the literal string 'me' in NestJS?
14How do you handle multi-value query parameters (arrays) in NestJS?
15What is the difference between @Body(), @Body('field'), and using a full DTO class in NestJS?
16How do you implement a PATCH endpoint correctly with partial validation using PartialType in NestJS?
17How do you implement discriminated union body validation where the DTO shape depends on a type field in NestJS?
18How do you set HTTP status codes, response headers, and redirects in NestJS without using @Res()?
19What is the recommended file structure for versioned controllers in a NestJS project?
20How do wildcard and optional route segments work in NestJS?
21What is the difference between @Param('id') and @Param() with no argument in NestJS?
22How do you extract and type query parameters in NestJS including optional ones with defaults?
23How do you implement a standard paginated response envelope across all list endpoints in NestJS?
24How do you apply versioning at controller and method level in NestJS and how do you mark a route as version-neutral?
25How do you set up Swagger in a NestJS application and annotate your controllers?
20 / 25

How do wildcard and optional route segments work in NestJS?

Difficulty: 5/10
wildcard routes, optional parameters, routing precedence

NestJS uses path-to-regexp under Express. Wildcards match any segment using * and optional params are declared with a trailing ?. Wildcard routes must be declared after specific routes because NestJS matches routes in declaration order on Express. Fastify uses a radix tree that prefers static segments over parametric ones.

Wildcard and optional route patterns
Wildcard routing rules:
  1. 1

    Declare specific literal routes before wildcard param routes — NestJS matches in declaration order on Express.

  2. 2

    Fastify uses a radix tree that automatically prefers static routes over parametric ones.

  3. 3

    @Get('*') is a catch-all and should always be the last route in a controller.

  4. 4

    Optional params with ? make the segment optional — the param value is undefined when absent.

  5. 5

    Always test wildcard behavior on both Express and Fastify if you plan to switch platforms.

Scenario Questions

0-2 years experience

  1. 1You need to create an endpoint that matches any path under /files, such as /files/report.pdf or /files/images/photo.jpg. How would you define the route in a NestJS controller using a wildcard segment?
  2. 2If you declare a route like @Get('users/:id?') in NestJS, what URLs will match it and how does the optional segment affect the handler's parameters?
  3. 3What happens if you place a wildcard route before a more specific route in the same controller? Which one will be hit for /files/report.pdf?

2-5 years experience

  1. 1We have a feature where a GET /search/:term?/:page? should return results, but when the client omits the term the request fails with a 404. Walk me through how you'd debug the routing configuration.
  2. 2Explain the trade‑offs between using a wildcard route (e.g., @Get('api/**')) versus defining multiple explicit routes when building a versioned API. When might the wildcard cause unexpected behavior?
  3. 3During a refactor we introduced an optional segment to an existing route, and some existing clients started receiving 400 errors. What could cause that and how would you fix it?

5-8 years experience

  1. 1Our microservice gateway aggregates several NestJS services, each exposing wildcard routes for static assets. At high traffic we see route‑matching latency spikes. How would you investigate and mitigate performance issues related to wildcard/optional segments?
  2. 2Design a routing strategy for a multi‑tenant SaaS app where each tenant can have optional sub‑paths (e.g., /:tenantId/dashboard) and also support catch‑all routes for custom pages. What considerations around precedence and security would you apply?
  3. 3If you need to migrate from a legacy Express router that uses regex wildcards to NestJS decorators, what pitfalls around optional parameters should you watch for to avoid breaking existing URLs?

8+ years experience

  1. 1Our organization is consolidating dozens of NestJS services into a monorepo. Several services expose overlapping wildcard routes, leading to ambiguous matches at the API gateway. How would you establish a cross‑team routing contract to prevent collisions and ensure future extensibility?
  2. 2Discuss the long‑term maintenance implications of relying heavily on wildcard routes for feature toggles versus explicit versioned endpoints. How would you guide teams to evolve the routing layer without accruing technical debt?
  3. 3When planning a migration to GraphQL, how would you decide which existing wildcard/optional REST routes can be safely deprecated, and what patterns would you use to map them to GraphQL resolvers?

Follow-up Questions

  • How does NestJS prioritize routes when both a wildcard and a concrete path could match?
  • What would you return from the handler if an optional parameter is missing?
  • Can you think of a scenario where a wildcard route could unintentionally expose internal endpoints?
Share

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