Created
November 8, 2022 19:21
-
-
Save BetterProgramming/72f085b3492966ba09c750a25373953a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Register the route to return all the animals | |
router.get('/animals', async (req: IttyRequest, env: Env) => { | |
let animals = await getAllAnimals(env) | |
return new Response(JSON.stringify(animals)) | |
}) | |
// Register the route to return an animal by ID | |
router.get('/animals/:id', async (req: IttyRequest, env: Env) => { | |
let animals = await getAllAnimals(env) | |
let animal = animals.find(a => a['id'] == Number(req.params?.id)) | |
return new Response(JSON.stringify(animal)) | |
}) | |
// Register the route to create an animal | |
router.post('/animals', async (req: IttyRequest, env: Env) => { | |
let content = await req.json?.() | |
if (content == undefined) { | |
return new Response('Please provide a body.') | |
} | |
//Do not use this in production, use a UUID, using Date.now() for convenience | |
content['id'] = Date.now() | |
let animal = await addAnimal(content, env) | |
return new Response(JSON.stringify(animal)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment