Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created November 8, 2022 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/72f085b3492966ba09c750a25373953a to your computer and use it in GitHub Desktop.
Save BetterProgramming/72f085b3492966ba09c750a25373953a to your computer and use it in GitHub Desktop.
// 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