Denoの特徴
- JavascriptとTypescriptの実行環境
- Typescriptのコンパイラを内包している(tsc-,ts-nodeなど不要)
- 標準ライブラリーは非同期処理にPromiseを採用
- 実行時に必要な権限のオプション指定が必要
- モジュールの扱いがNode.jsと異なる
- etc
環境構築
Docker
こちらのイメージを使用します。
https://hub.docker.com/r/hayd/deno
Dockerfile
FROM hayd/alpine-deno:1.2.2# Prefer not to run as root.USER deno# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.COPY deps.ts .RUN deno cache deps.ts
# These steps will be re-run upon each file change in your working directory:ADD . .# Compile the main app so that it doesn't need to be compiled each startup/entry.RUN deno cache main.ts
docker-compose.yaml
version:'3'services:app:build:.command:"run--allow-net--allow-readmain.ts"ports:-"8000:8000"volumes:-.:/appworking_dir:"/app"
HelloWorld
main.ts
import{serve}from"./deps.ts";constPORT=8000;consts=serve(`0.0.0.0:${PORT}`);constbody=newTextEncoder().encode("Hello World\n");console.log(`Server started on port ${PORT}`);forawait(constreqofs){req.respond({body});}
$ docker-compose up -d
APIの作成
Servestを使用
https://github.com/keroxp/servest
main.ts
import{createApp}from"https://servestjs.org/@v1.0.0/mod.ts";constapp=createApp();app.handle("/",async(req)=>{awaitreq.respond({status:200,headers:newHeaders({"content-type":"text/plain",}),body:"hello World!",});});app.listen({port:8000});