0
0
Expressframework~20 mins

Controller pattern for route handlers in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Controller Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when calling the Express route with the controller pattern?

Consider this Express route setup using a controller function:

const express = require('express');
const app = express();

const userController = {
  getUser: (req, res) => {
    res.send(`User ID is ${req.params.id}`);
  }
};

app.get('/user/:id', userController.getUser);

// Simulate a GET request to /user/42

What will the response body be?

Express
const express = require('express');
const app = express();

const userController = {
  getUser: (req, res) => {
    res.send(`User ID is ${req.params.id}`);
  }
};

app.get('/user/:id', userController.getUser);

// Simulate a GET request to /user/42
AUser ID is 42
BUser ID is undefined
CError: req.params is not defined
DUser ID is :id
Attempts:
2 left
💡 Hint

Think about how Express passes route parameters to the controller function.

📝 Syntax
intermediate
2:00remaining
Which option correctly exports a controller function for Express?

You want to create a controller file userController.js that exports a function getUser to handle requests.

Which code snippet correctly exports the function for use in Express routes?

Aexports.getUser() { res.send('Hello'); }
Bmodule.exports = { getUser: (req, res) => { res.send('Hello'); } };
Cexport getUser = (req, res) => { res.send('Hello'); };
Dmodule.exports.getUser = function(req, res) { res.send('Hello'); }
Attempts:
2 left
💡 Hint

Remember the Node.js module export syntax.

🔧 Debug
advanced
2:00remaining
Why does this controller function cause an error when used as a route handler?

Look at this controller code:

const userController = {
  getUser(req, res) {
    this.findUser(req.params.id).then(user => {
      res.json(user);
    });
  },
  findUser(id) {
    return Promise.resolve({ id, name: 'Alice' });
  }
};

app.get('/user/:id', userController.getUser);

When calling /user/1, it throws an error. Why?

Express
const userController = {
  getUser(req, res) {
    this.findUser(req.params.id).then(user => {
      res.json(user);
    });
  },
  findUser(id) {
    return Promise.resolve({ id, name: 'Alice' });
  }
};

app.get('/user/:id', userController.getUser);
AfindUser is not a function error because it is not defined
Bres.json is not a function error because res is undefined
CPromise is not defined error due to missing import
D'this' is undefined inside getUser when called by Express, causing a TypeError
Attempts:
2 left
💡 Hint

Think about how this behaves in JavaScript when a method is passed as a callback.

state_output
advanced
2:00remaining
What is the output after calling this Express controller with async/await?

Given this controller function:

const userController = {
  async getUser(req, res) {
    const user = await this.findUser(req.params.id);
    res.json(user);
  },
  findUser(id) {
    return Promise.resolve({ id, name: 'Bob' });
  }
};

app.get('/user/:id', userController.getUser.bind(userController));

// Simulate GET /user/7

What will the response JSON be?

Express
const userController = {
  async getUser(req, res) {
    const user = await this.findUser(req.params.id);
    res.json(user);
  },
  findUser(id) {
    return Promise.resolve({ id, name: 'Bob' });
  }
};

app.get('/user/:id', userController.getUser.bind(userController));

// Simulate GET /user/7
A{"id":"7","name":"Bob"}
B{"id":7,"name":"Bob"}
C{"id":"req.params.id","name":"Bob"}
DError: Cannot read property 'findUser' of undefined
Attempts:
2 left
💡 Hint

Remember that route parameters are strings by default.

🧠 Conceptual
expert
2:00remaining
Which statement best explains the benefit of using the controller pattern in Express route handlers?

Why do developers use the controller pattern to organize route handlers in Express applications?

AIt forces all routes to be synchronous, preventing async bugs
BIt automatically improves server performance by caching responses
CIt separates route definitions from business logic, making code easier to maintain and test
DIt eliminates the need for middleware functions in Express
Attempts:
2 left
💡 Hint

Think about code organization and clarity.