1
0

More than 3 years have passed since last update.

Deno触ってみた

Last updated at Posted at 2020-12-14

初めに

はい、こんにちは。LinuxClubアドベントカレンダー2020 13日目担当のMikanerです。
アドベントカレンダー用の記事の内容が何も思いつかなかったので下書きに放置されて温めておいたDenoを記事にします。

なんでDeno?

恐竜がかわいかった。

環境

image.png
今回はpowershellでやります。

とりあえずinstall

Denoのページにいってコピペ。
https://deno.land/

Powershellにiwrってコマンドあったんだね。知らなかった。

> iwr https://deno.land/x/install/install.ps1 -useb | iex

これだけ。めっちゃ簡単。

早速使ってみる

なんか打つと起動するらしいので打ってみる。

コマンド
> deno run https://deno.land/std/examples/welcome.ts
出力
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using master branch https://deno.land/std/examples/welcome.ts
Compile https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

わぁい!恐竜さんだ!!!

TCPサーバを立ててみる

Denoの公式マニュアルにTCPサーバのコードがあったのでやってみる。

tcp_server.ts
const hostname = "0.0.0.0";
const port = 8080;
const listener = Deno.listen({ hostname, port });
console.log(`Listening on ${hostname}:${port}`);
for await (const conn of listener) {
  Deno.copy(conn, conn);
}
server用のpowershell
> deno run --allow-net .\tcp_server.ts
Check file:///C:/Users/ごにょごにょ/tcp_server.ts
Listening on 0.0.0.0:8080

powershellにncがなかったのでここのコードをコピペ参考にnc.ps1ファイルを書いて実行

client用のpowershell
> powershell -ex remotesigned -c ".\nc -v localhost 8080"
詳細: Connection to localhost 8080 port [tcp/*] succeeded!
hello

hello

何故か送信するとひとつ前のレスポンスが返ってくるけどまあ動いているのでよし。

Webサーバを立てる

Webサーバのサンプルがあったので使ってみる。

web_server.ts
import { serve } from "https://deno.land/std@0.80.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000");
for await (const req of s) {
    req.respond({ body: "Hello world!! 🦕"});
}
server用のpowershell
> deno run --allow-net .\web_server.ts
Download https://deno.land/std@0.80.0/http/server.ts
Download https://deno.land/std@0.80.0/encoding/utf8.ts
Download https://deno.land/std@0.80.0/async/mod.ts
Download https://deno.land/std@0.80.0/_util/assert.ts
Download https://deno.land/std@0.80.0/http/_io.ts
Download https://deno.land/std@0.80.0/bytes/mod.ts
Download https://deno.land/std@0.80.0/http/http_status.ts
Download https://deno.land/std@0.80.0/textproto/mod.ts
Download https://deno.land/std@0.80.0/async/deferred.ts
Download https://deno.land/std@0.80.0/async/pool.ts
Download https://deno.land/std@0.80.0/async/delay.ts
Download https://deno.land/std@0.80.0/async/mux_async_iterator.ts
Check file:///C:/Users/ごにょごにょ/web_server.ts
http://localhost:8000

実行結果

image.png

文字化けしてんねぇ....

文字化け回避

とりあえずHTMLのmetaタグで指定しとけばいいでしょ。

web_server.ts
import { serve } from "https://deno.land/std@0.80.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000");
for await (const req of s) {
    req.respond({ body: "<html><head><meta charset=\"utf-8\"/></head><body>Hello world!! 🦕</body></html>"});
}

恐竜見えた!

image.png

最高!

参考文献

First step - Manual | Deno, 閲覧日:2020/12/15, https://deno.land/manual@v1.6.0/getting_started/first_steps
std@0.80.0/http | Deno, 閲覧日:2020/12/15, https://deno.land/std@0.80.0/http
PowerShellでnc(netcat)を書いてみる - ももいろテクノロジー, 閲覧日:2020/12/15, http://inaz2.hatenablog.com/entry/2015/04/16/025953

1
0
1

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