Catch-all API routes let you handle many URL paths with one file. This helps when you want to respond to different paths without making many files.
Catch-all API routes in NextJS
Introduction
Syntax
NextJS
pages/api/[...slug].js export default function handler(req, res) { const { slug } = req.query; res.status(200).json({ slug }); }
The file name uses square brackets with three dots: [...slug].
The slug is an array of all parts of the URL after /api/.
Examples
params as the catch-all name. It returns all URL parts as an array.NextJS
pages/api/[...params].js export default function handler(req, res) { const { params } = req.query; res.status(200).json({ params }); }
blog folder, catching all paths under /api/blog/.NextJS
pages/api/blog/[...slug].js export default function handler(req, res) { const { slug } = req.query; res.status(200).json({ slug }); }
all as the parameter name to catch all API paths.NextJS
pages/api/[...all].js export default function handler(req, res) { const { all } = req.query; res.status(200).json({ all }); }
Sample Program
This API route catches all paths under /api/[...slug]. It returns a JSON with a message and the array of URL parts.
NextJS
export default function handler(req, res) { const { slug } = req.query; res.status(200).json({ message: 'Catch-all route works!', slug }); }
Important Notes
If the URL is /api/posts/2024/06, the slug array will be ["posts", "2024", "06"].
You can also create optional catch-all routes using [[...slug]].js to allow empty paths.
Catch-all routes help keep your API organized and flexible.
Summary
Catch-all API routes use [...name].js to catch many URL parts.
The caught parts come as an array in req.query.
This makes handling dynamic and nested API paths easy.
Practice
1. What does a catch-all API route file named
[...slug].js in Next.js do?easy
Solution
Step 1: Understand catch-all route naming
In Next.js, files named with[...name].jscatch all nested paths under that route.Step 2: Check how parameters are passed
The parts of the URL matched are passed as an array inreq.query.name, hereslug.Final Answer:
It catches all URL paths that match and provides the parts as an array in req.query.slug. -> Option AQuick Check:
Catch-all route = array in req.query [OK]
Hint: Catch-all uses [...name].js and returns array in req.query[name] [OK]
Common Mistakes:
- Thinking it catches only one path instead of many
- Expecting a string instead of an array in req.query
- Confusing catch-all with optional catch-all routes
2. Which of the following is the correct filename for a catch-all API route in Next.js?
easy
Solution
Step 1: Recall catch-all route syntax
Catch-all routes use three dots inside square brackets before the name, like[...slug].js.Step 2: Compare options
Only [...slug].js matches the correct syntax for catch-all routes.Final Answer:
[...slug].js -> Option BQuick Check:
Catch-all filename = [...name].js [OK]
Hint: Catch-all filenames always have three dots inside brackets [OK]
Common Mistakes:
- Using single brackets without dots for catch-all
- Placing dots outside brackets
- Missing brackets entirely
3. Given a catch-all API route file
What will be the JSON response for a request to
pages/api/[...params].js with this handler:export default function handler(req, res) {
res.json(req.query.params);
}What will be the JSON response for a request to
/api/a/b/c?medium
Solution
Step 1: Understand catch-all parameter passing
The catch-all[...params]captures all path parts as an array inreq.query.params.Step 2: Check the handler response
The handler returnsreq.query.paramsas JSON, so the response is the array of parts.Final Answer:
["a", "b", "c"] -> Option CQuick Check:
Catch-all returns array of path parts [OK]
Hint: Catch-all returns array of URL parts in req.query [OK]
Common Mistakes:
- Expecting a string instead of an array
- Confusing JSON structure returned
- Not recognizing catch-all returns array
4. You wrote a catch-all API route
pages/api/[...slug].js but when you request /api (no extra path), you get an error. What is the likely cause?medium
Solution
Step 1: Understand catch-all vs optional catch-all
Catch-all[...slug].jsrequires at least one path part; it does not match the base path alone.Step 2: Identify fix for base path requests
To handle base path/apiwith no extra parts, use optional catch-all[[...slug]].js.Final Answer:
Catch-all routes require at least one path segment; use optional catch-all [[...slug]].js instead. -> Option DQuick Check:
Catch-all needs path parts; optional catch-all handles empty [OK]
Hint: Use [[...name]].js for optional catch-all including empty path [OK]
Common Mistakes:
- Expecting catch-all to match empty path
- Misnaming the file without brackets
- Ignoring export of handler function
5. You want to create a catch-all API route that returns a JSON object with keys for each path part and their index, for example, for
/api/a/b/c it returns {"0": "a", "1": "b", "2": "c"}. Which handler code inside pages/api/[...parts].js achieves this?hard
Solution
Step 1: Understand the input and desired output
The catch-all parameterpartsis an array of path parts. We want to create an object with keys as indices and values as parts.Step 2: Check which code builds the object correctly
export default function handler(req, res) { const obj = {}; req.query.parts.forEach((p, i) => { obj[i] = p; }); res.json(obj); } loops overreq.query.partsand assigns each part to the object with its index as key, then returns it as JSON.Final Answer:
export default function handler(req, res) { const obj = {}; req.query.parts.forEach((p, i) => { obj[i] = p; }); res.json(obj); } -> Option AQuick Check:
Loop parts to object with index keys = export default function handler(req, res) { const obj = {}; req.query.parts.forEach((p, i) => { obj[i] = p; }); res.json(obj); } [OK]
Hint: Use forEach to map array parts to object keys [OK]
Common Mistakes:
- Returning array or string instead of object
- Not looping to create object keys
- Returning length instead of parts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
