0
0
NestJSframework~20 mins

Creating middleware in NestJS - Practice Exercises

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
What is the output of this NestJS middleware logging example?
Consider this NestJS middleware that logs the request method and URL. What will be logged when a GET request is made to '/users'?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log(`${req.method} ${req.url}`);
    next();
  }
}
APOST /users
BGET /users
CError: req.method is undefined
DGET undefined
Attempts:
2 left
💡 Hint
Look at how req.method and req.url are used inside the middleware.
lifecycle
intermediate
1:30remaining
When is NestJS middleware executed in the request lifecycle?
At what point in the request lifecycle does NestJS middleware run?
AOnly after the response is sent to the client
BAfter the route handler returns a response
CBefore the route handler is called
DOnly during application startup
Attempts:
2 left
💡 Hint
Middleware typically processes requests before controllers handle them.
📝 Syntax
advanced
2:30remaining
Which option correctly applies middleware to a specific route in NestJS?
You want to apply a middleware only to the '/api/products' route. Which code snippet correctly does this in NestJS?
NestJS
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { LoggerMiddleware } from './logger.middleware';

@Module({})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // Choose the correct option below
  }
}
Aconsumer.apply(LoggerMiddleware).forRoutes('/api/products');
Bconsumer.use(LoggerMiddleware).forRoutes('/api/products');
Cconsumer.apply(LoggerMiddleware).toRoutes('/api/products');
Dconsumer.use(LoggerMiddleware).toRoutes('/api/products');
Attempts:
2 left
💡 Hint
Check the NestJS MiddlewareConsumer API for the correct method names.
🔧 Debug
advanced
2:00remaining
Why does this NestJS middleware not log anything?
This middleware is supposed to log request info but logs nothing. What is the cause?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class SilentMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    // Missing console.log here
    next();
  }
}
AThe middleware does not call console.log, so nothing is logged
BThe middleware forgot to call next(), so it hangs
CThe middleware is not decorated with @Injectable()
DThe middleware uses wrong parameter types
Attempts:
2 left
💡 Hint
Look inside the use method for logging statements.
🧠 Conceptual
expert
2:00remaining
What happens if NestJS middleware does not call next()?
In NestJS, what is the effect of middleware that does not call the next() function?
AThe request will skip the middleware and go directly to the route handler
BThe middleware will complete and send a default response
CThe middleware will throw a runtime error automatically
DThe request will hang and never reach the route handler
Attempts:
2 left
💡 Hint
Think about how middleware chains work in Express-based frameworks.