Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
If you define `router.get(
š» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a Node Express Routes and Controllers pipeline. Include the setup and basic execution steps.
You are reviewing a Node Express Routes and Controllers pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Mounting router.use(requireAuth) below a route that should have been protected
// Wrong: /profile registered BEFORE the auth middleware
router.get('/profile', getProfile); // unprotected!
router.use(requireAuth);
// Correct: middleware first, protected routes after
router.use(requireAuth);
router.get('/profile', getProfile);The Solution //
router.use() middleware only applies to routes registered AFTER it in the same file ā Express evaluates route registration order, not the eventual file structure. A protected route accidentally defined above the router.use(requireAuth) line executes with zero authentication, silently exposing it.
The Error //
An async controller throwing an error crashes the process instead of returning a JSON error
// Wrong: a thrown/rejected error here is never caught by Express
router.get('/:id', async (req, res) => {
const user = await db.findById(req.params.id); // throws if DB is down
res.json(user);
});
// Correct
router.get('/:id', async (req, res, next) => {
try {
const user = await db.findById(req.params.id);
res.json(user);
} catch (err) {
next(err);
}
});The Solution //
Express route handlers don't automatically catch rejected Promises from async functions ā an unhandled rejection inside an async controller bypasses Express's default error handling entirely and can crash the process. Wrap async controller logic in try/catch and call next(error), or use a wrapper (like express-async-handler) that forwards rejections to next() automatically.