高木のブログ

【Deno】Deno Deployで動かすアプリの開発メモ

· 73 words · 1 minutes to read

前にDeno Deployを試した記事(Deno Deployを試してみた )を書いたけど、開発の部分を書いてなくてちょっとしたものをサクッと作ろうとした時に困ったのでメモしておく
まあこのくらいなら公式ドキュメントを見ればいいのだけど

CLI 🔗

ローカルで開発する時はdeployctlというCLIを使用する

インストール 🔗

$ deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -f https://deno.land/x/deploy/deployctl.ts
✅ Successfully installed deployctl
/Users/ytkg/.deno/bin/deployctl

$ deployctl --version
deployctl 0.3.0

サーバー起動 🔗

コードに変更があったらリロードしてくれるので、--watchオプションはつけておいた方が良い

$ deployctl run --watch app.js
Listening on http://0.0.0.0:8080

コード 🔗

シンプルに文字列でユーザーエージェントを返すサンプルコード

function handleRequest(request) {
  const userAgent = request.headers.get("user-agent");

  return new Response(`User Agent: ${userAgent}\n`, {
    headers: { "content-type": "text/plain" },
  });
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
$ curl localhost:8080
User Agent: curl/7.64.1

Categories


Tags