0
0
NestJSframework~20 mins

Middleware ordering in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Order of Middleware Execution in NestJS
Given the following NestJS middleware setup, what will be the order of console logs when a request hits the '/test' route?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
class FirstMiddleware implements NestMiddleware {
  use(req, res, next) {
    console.log('First');
    next();
  }
}

@Injectable()
class SecondMiddleware implements NestMiddleware {
  use(req, res, next) {
    console.log('Second');
    next();
  }
}

// In AppModule
export class AppModule {
  configure(consumer) {
    consumer
      .apply(FirstMiddleware, SecondMiddleware)
      .forRoutes('/test');
  }
}
AFirst
BSecond\nFirst
CSecond
DFirst\nSecond
Attempts:
2 left
💡 Hint
Middleware are executed in the order they are applied in the consumer.
lifecycle
intermediate
2:00remaining
Middleware Execution with next() Skipped
What happens if a middleware in NestJS does NOT call next() in its use() method?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
class BlockMiddleware implements NestMiddleware {
  use(req, res, next) {
    res.status(403).send('Blocked');
    // next() is NOT called
  }
}

// Applied as the only middleware on '/block' route
AAn error is thrown because next() is missing.
BThe request stops at BlockMiddleware and response 'Blocked' is sent; no further middleware or route handlers run.
CThe request continues to next middleware or route handler despite missing next().
DThe server crashes due to unhandled middleware.
Attempts:
2 left
💡 Hint
Calling next() passes control to the next middleware or handler.
🔧 Debug
advanced
2:00remaining
Unexpected Middleware Execution Order
You applied two middleware in this order: AuthMiddleware, LoggerMiddleware. But the logs show LoggerMiddleware runs before AuthMiddleware. What is the most likely cause?
NestJS
consumer
  .apply(AuthMiddleware, LoggerMiddleware)
  .forRoutes('*');
ALoggerMiddleware was globally applied before this consumer.apply call, so it runs first.
BMiddleware order in apply() is reversed internally by NestJS.
CMiddleware with longer names run later automatically.
DAuthMiddleware calls next() asynchronously, so LoggerMiddleware runs first.
Attempts:
2 left
💡 Hint
Global middleware run before route-specific middleware.
📝 Syntax
advanced
2:00remaining
Correct Middleware Application Syntax
Which option correctly applies two middleware to the '/api' route in NestJS?
Aconsumer.use(AuthMiddleware, LoggerMiddleware).forRoutes('/api');
Bconsumer.apply([AuthMiddleware, LoggerMiddleware]).forRoutes('/api');
Cconsumer.apply(AuthMiddleware, LoggerMiddleware).forRoutes('/api');
Dconsumer.apply(AuthMiddleware).apply(LoggerMiddleware).forRoutes('/api');
Attempts:
2 left
💡 Hint
apply() accepts middleware as separate arguments, not as an array.
state_output
expert
3:00remaining
Middleware Modifying Request Object
Consider two middleware applied in order: AddUserMiddleware adds req.user = {id: 1}, then LogUserMiddleware logs req.user. What will LogUserMiddleware output?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
class AddUserMiddleware implements NestMiddleware {
  use(req, res, next) {
    req.user = { id: 1 };
    next();
  }
}

@Injectable()
class LogUserMiddleware implements NestMiddleware {
  use(req, res, next) {
    console.log(req.user);
    next();
  }
}

// Applied as consumer.apply(AddUserMiddleware, LogUserMiddleware).forRoutes('*');
A{ id: 1 }
Bundefined
Cnull
DThrows ReferenceError
Attempts:
2 left
💡 Hint
Middleware run in order and share the same request object.