So, I was setting up some routes for my API & I had to setup some error fallbacks. And I figured out a fun and dynamic way of doing this, so instead of sending a response message to the endpoint I decided to redirect the user to a dynamic status page. Here's how I did it. (This is for NextJS)
Make a directory in /pages/ called "status", then make a .js file named [code].js & inside that code I use this:
import { useRouter } from "next/router";
export default function NodeStatus() {
const router = useRouter();
const { status } = router.query;
return (
<div className="node__status__page">
<img
src={`https://http.cat/${status}`}
className="w-full h-full max-w-lg"
/>
</div>
);
}
Then the way I handle the request depending on what error it is I use this simple line:
res.status(500).redirect(`/status/${res.statusCode}`);
I know it's not very beautiful code but I hope someone will see it as useful! ^^ Thank you!
Top comments (0)