0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

oak のサンプル (POST の処理)

Posted at

次のプログラムを改造して 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?