LoginSignup
0
2

More than 3 years have passed since last update.

さくっとDenoを試してみる

Posted at

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-read main.ts"
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    working_dir: "/app"

HelloWorld

main.ts
import { serve } from "./deps.ts";

const PORT =8000;
const s = serve(`0.0.0.0:${PORT}`);
const body = new TextEncoder().encode("Hello World\n");

console.log(`Server started on port ${PORT}`);
for await (const req of s) {
  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";
const app = createApp();
app.handle("/", async (req) => {
  await req.respond({
    status: 200,
    headers: new Headers({
      "content-type": "text/plain",
    }),
    body: "hello World!",
  });
});
app.listen({ port:8000 });

参考

0
2
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
2