0
0
NextJSframework~20 mins

Catch-all API routes in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Catch-all 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 catch-all API route?
Consider a Next.js API route file named pages/api/[...slug].js with the following code. What will be the JSON response when a GET request is made to /api/a/b/c?
NextJS
export default function handler(req, res) {
  const { slug } = req.query;
  res.status(200).json({ path: slug });
}
A{"path": ["a", "b", "c"]}
B{"path": "a/b/c"}
C{"path": null}
D{"path": []}
Attempts:
2 left
💡 Hint
Remember that catch-all routes capture all parts of the URL as an array in the query.
📝 Syntax
intermediate
1:30remaining
Which catch-all API route filename is valid in Next.js?
Which of the following filenames correctly defines a catch-all API route in Next.js?
A[...slug].jsx
B[slug...].js
C[...slug].js
D[slug].js
Attempts:
2 left
💡 Hint
Catch-all routes use three dots inside square brackets before the name.
🔧 Debug
advanced
2:30remaining
Why does this catch-all API route return 404 for /api?
Given a catch-all API route file pages/api/[...params].js with this code:
export default function handler(req, res) {
  const { params } = req.query;
  res.status(200).json({ params });
}
Why does a GET request to /api return 404 Not Found?
ABecause the handler function is missing a return statement.
BBecause catch-all routes do not match the base path without any segments.
CBecause req.query.params is undefined and causes an error.
DBecause Next.js requires a separate file for the base /api route.
Attempts:
2 left
💡 Hint
Think about how catch-all routes match URLs with zero or more segments.
state_output
advanced
2:00remaining
What is the value of req.query.slug for /api/foo?
In a Next.js API route file named pages/api/[...slug].js, what is the value of req.query.slug when a GET request is made to /api/foo?
NextJS
export default function handler(req, res) {
  res.status(200).json({ slug: req.query.slug });
}
A[]
B"foo"
Cundefined
D["foo"]
Attempts:
2 left
💡 Hint
Catch-all routes always return an array for the matched segments.
🧠 Conceptual
expert
3:00remaining
How to handle both /api and /api/* with catch-all routes?
You want to create a Next.js API that handles requests to /api and all sub-paths like /api/a/b using catch-all routes. Which setup correctly handles both cases?
ACreate <code>pages/api/[[...slug]].js</code> which matches /api and all sub-paths.
BCreate only <code>pages/api/[...slug].js</code> and check if slug is undefined for /api.
CCreate <code>pages/api/index.js</code> for /api and <code>pages/api/[...slug].js</code> for all sub-paths.
DCreate <code>pages/api/[slug].js</code> and <code>pages/api/[...slug].js</code> to cover all cases.
Attempts:
2 left
💡 Hint
Next.js supports optional catch-all routes with double brackets.