0
0
Expressframework~20 mins

Dependency injection in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main benefit of using dependency injection in Express?

Why do developers use dependency injection in Express applications?

ATo tightly couple modules so they depend directly on each other
BTo make testing easier by allowing dependencies to be replaced or mocked
CTo increase the size of the application bundle for better performance
DTo avoid using middleware functions in the request pipeline
Attempts:
2 left
💡 Hint

Think about how you can swap parts of your app during tests.

component_behavior
intermediate
2:00remaining
What will this Express middleware output when dependency injection is used correctly?

Consider this middleware that uses a service injected via req.services. What will it send as a response?

Express
app.use((req, res, next) => {
  const message = req.services.greetingService.getMessage();
  res.send(message);
});
A"Hello from the greeting service!"
B"Cannot read property 'getMessage' of undefined" error
C"undefined"
D"Hello World"
Attempts:
2 left
💡 Hint

Assume req.services.greetingService is properly injected and has a getMessage method returning a string.

📝 Syntax
advanced
2:00remaining
Which option correctly injects a service into Express request objects?

Choose the code snippet that properly adds a userService to each request using middleware.

A
app.use((req, res, next) => {
  req.userService = new UserService();
  next();
});
B
app.use((req, res, next) => {
  res.userService = new UserService();
  next();
});
C
app.use((req, res, next) => {
  next.userService = new UserService();
  next();
});
D
app.use((req, res, next) => {
  UserService = new UserService();
  next();
});
Attempts:
2 left
💡 Hint

Remember that req is the request object passed to handlers.

🔧 Debug
advanced
2:00remaining
Why does this injected service cause an error in Express?

Given this middleware, why does accessing req.services.dataService cause a TypeError?

Express
app.use((req, res, next) => {
  req.services.dataService = new DataService();
  next();
});

app.get('/', (req, res) => {
  res.send(req.services.dataService.getData());
});
A<code>next()</code> is not called in the middleware, blocking the request
B<code>DataService</code> is not defined anywhere in the code
CThe <code>getData()</code> method does not return a string
D<code>req.services</code> is undefined, so assigning <code>dataService</code> fails
Attempts:
2 left
💡 Hint

Check if req.services exists before assigning properties.

state_output
expert
2:00remaining
What is the output of this Express app using dependency injection with a singleton service?

Consider this code where a singleton service counts requests. What will the response be after three requests?

Express
class CounterService {
  constructor() {
    this.count = 0;
  }
  increment() {
    this.count++;
    return this.count;
  }
}

const counterService = new CounterService();

app.use((req, res, next) => {
  req.counter = counterService;
  next();
});

app.get('/', (req, res) => {
  const currentCount = req.counter.increment();
  res.send(`Request number: ${currentCount}`);
});
ARequest number: 0
BRequest number: 1
CRequest number: 3
DRequest number: undefined
Attempts:
2 left
💡 Hint

Think about how the singleton service keeps state across requests.