Complete the code to define a dynamic API route file name for a user ID.
export default function handler(req, res) {
const { [1] } = req.query;
res.status(200).json({ id: [1] });
}req.query.req.query.The dynamic route parameter is accessed via req.query.id when the file is named [id].js.
Complete the code to create a dynamic API route file that captures a post slug.
export default function handler(req, res) {
const { [1] } = req.query;
res.status(200).json({ slug: [1] });
}The file should be named [slug].js to capture the slug parameter, accessed as req.query.slug.
Fix the error in accessing the dynamic route parameter in this API handler.
export default function handler(req, res) {
const id = req.[1].id;
res.status(200).json({ id });
}req.params which is undefined in Next.js API routes.req.query.In Next.js API routes, dynamic parameters are accessed via req.query, not req.params.
Fill both blanks to correctly extract and respond with a dynamic user ID and post ID from the API route.
export default function handler(req, res) {
const { [1], [2] } = req.query;
res.status(200).json({ userId: [1], postId: [2] });
}id or slug instead of the correct keys.The dynamic route file might be named [userId]/[postId].js, so the parameters are userId and postId in req.query.
Fill all three blanks to create a dynamic API route handler that returns the category, subcategory, and item ID.
export default function handler(req, res) {
const { [1], [2], [3] } = req.query;
res.status(200).json({ category: [1], subcategory: [2], itemId: [3] });
}id instead of specific dynamic segment names.For a dynamic route like [category]/[subcategory]/[itemId].js, the parameters are category, subcategory, and itemId in req.query.