DEV Community

Anjan Talatam
Anjan Talatam

Posted on

1

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)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay