0
0
NextJSframework~20 mins

Dynamic API routes in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic API Routes Master
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 dynamic API route?
Consider a Next.js API route file named pages/api/user/[id].js with the following code. What will be the JSON response when a GET request is made to /api/user/42?
NextJS
export default function handler(req, res) {
  const { id } = req.query;
  res.status(200).json({ userId: id, message: `User ID is ${id}` });
}
A{"id":"42","message":"User ID is 42"}
B{"userId":42,"message":"User ID is 42"}
C{"userId":"42","message":"User ID is 42"}
D{"userId":"id","message":"User ID is id"}
Attempts:
2 left
💡 Hint
Remember that query parameters in Next.js API routes are always strings.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a catch-all dynamic API route?
You want to create a Next.js API route that catches all paths under /api/posts/, including nested paths like /api/posts/2023/05. Which file name correctly implements this?
Apages/api/posts/[...slug].jsx
Bpages/api/posts/[slug].js
Cpages/api/posts/[slug...].js
Dpages/api/posts/[...slug].js
Attempts:
2 left
💡 Hint
Catch-all routes use three dots inside square brackets.
🔧 Debug
advanced
2:00remaining
Why does this dynamic API route return 404 for /api/products/123?
Given the file pages/api/product/[productId].js with this code, why does a request to /api/products/123 return 404?
export default function handler(req, res) {
  const { productId } = req.query;
  if (!productId) {
    res.status(400).json({ error: 'Missing productId' });
    return;
  }
  res.status(200).json({ id: productId });
}
AThe handler does not handle POST requests, so GET returns 404.
BThe file is inside pages/api/product/ but the folder is named 'product' instead of 'products'.
CThe dynamic segment is named [id], but code uses productId.
DThe code returns 400 if productId is missing, but productId is always undefined.
Attempts:
2 left
💡 Hint
Check the folder name in the file path carefully.
state_output
advanced
2:00remaining
What is the value of slug in this catch-all API route?
In a Next.js API route file named pages/api/blog/[...slug].js, the handler is:
export default function handler(req, res) {
  const { slug } = req.query;
  res.status(200).json({ slug });
}
What is the JSON response when a request is made to /api/blog/2024/06/updates?
A{"slug":["2024","06","updates"]}
B{"slug":"2024/06/updates"}
C{"slug":["2024/06/updates"]}
D{"slug":null}
Attempts:
2 left
💡 Hint
Catch-all routes provide an array of path segments.
🧠 Conceptual
expert
2:00remaining
Which statement about dynamic API routes in Next.js is TRUE?
Select the correct statement about dynamic API routes in Next.js.
ACatch-all dynamic API routes capture multiple path segments as an array in req.query.
BDynamic API routes require manual parsing of URL parameters from req.url.
CDynamic API routes can only have one dynamic segment per file path.
DOptional catch-all routes use the syntax [..param] without three dots.
Attempts:
2 left
💡 Hint
Think about how Next.js handles multiple path segments in dynamic routes.