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/42What will the response body be?
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
Think about how Express passes route parameters to the controller function.
The controller function receives the request object with params parsed from the URL. The id parameter is extracted correctly, so the response sends 'User ID is 42'.
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?
Remember the Node.js module export syntax.
Option B correctly assigns an object with a function to module.exports.
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?
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);Think about how this behaves in JavaScript when a method is passed as a callback.
When Express calls the handler, this is not bound to userController. So this.findUser is undefined, causing a TypeError.
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/7What will the response JSON be?
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/7Remember that route parameters are strings by default.
The id from req.params.id is a string '7'. The object returned has id as string. JSON.stringify keeps it as string, so option A is correct if quotes are included. But option A shows id as number 7, which is incorrect because params are strings.
Why do developers use the controller pattern to organize route handlers in Express applications?
Think about code organization and clarity.
The controller pattern helps keep route definitions clean and moves the logic to separate controller files or objects. This improves maintainability and testing. The other options are incorrect or unrelated.