Itty Router is a tiny router package that has a similar API to Express. In this video, we install Itty Router and the Extras packages.
Additionally, we refactor our existing worker to use the router, and declare a dynamic route to fetch a single article.
To handle paths and articles that don't exist, we add a catch all route that returns a 404 status.
Lastly, we use the json
helper to automatically stringify JSON objects and set the Content-Type
header for application/json
.
Install Itty Router and extras
npm i itty-router itty-router-extras
Run wrangler development server
npx wrangler dev
GET route for all articles
router.get(
"/articles",
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY }) => {
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const { data } = await supabase.from("articles").select("*");
return json(data);
}
);
GET route for one article
router.get(
"/articles/:id",
async (request, { SUPABASE_URL, SUPABASE_ANON_KEY }) => {
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const { id } = request.params;
const { data } = await supabase
.from("articles")
.select("*")
.match({ id })
.single();
if (!data) {
return status(404, "Not found");
}
return json(data);
}
);
Catch all route for 404
router.all("*", () => status(404, "Not found"));