次のプログラムを改造して POST の処理をするようにしました。
oak のサンプル (その2)
oak_post01.ts
// ---------------------------------------------------------------
// oak_post01.ts
//
// May/20/2020
//
// ---------------------------------------------------------------
import { Application,Router } from "https://deno.land/x/oak/mod.ts"
const books = new Map<string, any>();
books.set("1", { id: "1", title: "それから", author: "夏目漱石", })
books.set("2", { id: "2", title: "夜明け前", author: "島崎藤村", })
books.set("3", { id: "3", title: "舞姫", author: "森鴎外", })
const router = new Router()
router
.post("/", (context) => {
context.response.body = "日本文学"
})
.post("/list", (context) => {
context.response.body = Array.from(books.values())
})
.post("/book", async context => {
const body = await context.request.body()
context.response.body = books.get(body.value.key)
})
const app = new Application()
app.use(router.routes())
app.use(router.allowedMethods())
console.log("*** start ***")
await app.listen({ port: 8000 })
// ---------------------------------------------------------------
実行方法
deno run --allow-net oak_post01.ts
総てのデータの取得
curl -X POST http://localhost:8000/list | jq .
key を与えてのデータの取得
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"1"}' http://localhost:8000/book
echo
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"2"}' http://localhost:8000/book
echo
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"3"}' http://localhost:8000/book
echo