DEV Community

Anjan Talatam
Anjan Talatam

Posted on

How to handle unregistered routes in Next JS API

By default unregistered api routes fallback to below screen.

Default handling of unregistered routes

We can handle this by using Catch-all Segments in Next

In the /api folder create a file [...rest].tsx Give it a name of your choice. ( here rest refers to rest of the routes )

Folder structure NextJS API

Now all unregistered routes can be handled in this file.

// [...rest].tsx

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
  res.status(404).json({ message: "Route not found" });
}
Enter fullscreen mode Exit fullscreen mode

handle unregistered routes

Edit

Top comments (0)