@@ -21,22 +21,46 @@ npm i -D @cloudflare/workers-types
2121yarn add -D @cloudflare/workers-types
2222```
2323
24- ## Write your code
24+ ## Example
2525
2626``` ts
2727// index.ts
2828import { Pico } from ' @picojs/pico'
2929
30+ // create Pico instance
3031const app = new Pico ()
3132
33+ // handle GET request and return TEXT response
3234app .get (' /' , () => ' Hello Pico!' )
33- app .get (' /entry/:id' , ({ pathname }) => {
35+
36+ // capture the path parameter and return JSON response
37+ app .post (' /entry/:id' , ({ pathname }) => {
3438 const { id } = pathname .groups
3539 return {
3640 ' your id is' : id ,
3741 }
3842})
3943
44+ // get the query parameter
45+ app .get (' /search' , ({ search }) => {
46+ const params = new URLSearchParams (search .input )
47+ return ` Your query is ${params .get (' q' )} `
48+ })
49+
50+ // handle the PURGE method and return Redirect response
51+ app .on (' PURGE' , ' /cache' , () => {
52+ return new Response (null , {
53+ status: 302 ,
54+ headers: {
55+ Location: ' /' ,
56+ },
57+ })
58+ })
59+
60+ // return custom 404 response
61+ app .all (' *' , () => new Response (' Custom 404' , { status: 404 })
62+
63+ // export app for Cloudflare Workers
4064export default app
4165` ` `
4266
@@ -52,6 +76,26 @@ wrangler dev index.ts
5276wrangler publish index .ts
5377` ` `
5478
79+ ## Bun
80+
81+ Now, Pico does not work on Bun because URLPattern may not be implemented yet.
82+
83+ ## Deno
84+
85+ ` ` ` ts
86+ import { serve } from ' https://deno.land/std/http/server.ts'
87+ import { Pico } from ' https://esm.sh/@picojs/pico'
88+
89+ const app = new Pico ()
90+ app .get (' /' , () => ' Hi Deno!' )
91+
92+ serve (app .fetch )
93+ ` ` `
94+
95+ ` ` `
96+ deno run - A pico .ts
97+ ` ` `
98+
5599## Q&A
56100
57101> What's the difference from Hono?
0 commit comments