LoginSignup
35
37

More than 3 years have passed since last update.

Deno 1.0.0 と Servest 1.0.0 で HelloWorld

Last updated at Posted at 2020-05-14

Deno

Deno https://deno.land/

asdf

  • いろいろパッケージマネージャ
  • プラグインを入れることでいろんなパッケージを入れることができる

インストール

git で ~/.asdf に落としてきて最新化

git clone https://github.com/asdf-vm/asdf.git ~/.asdf
cd ~/.asdf
git checkout "$(git describe --abbrev=0 --tags)"

.bash_profile.zshrc に以下を追加してシェルを再起動

. $HOME/.asdf/asdf.sh

Deno のパッケージを入れる

asdfを使用してDenoのバージョンを管理する 必見

asdf plugin-add deno https://github.com/asdf-community/asdf-deno.git
asdf install deno 1.0.0
asdf global deno 1.0.0

Deno Helloworld

公式サンプルそのままで、見た目普通の js に見えるけど、①package.json とか作らなくていいし、②npm は使わないし (標準ライブラリを https で読んでる)、③awaitasync で囲わなくていいみたいなのが読み取れる。

index.js
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";

for await (const req of serve({ port: 8000 })) {
  req.respond({ body: "Hello World\n" });
}

実行は deno run hoge。今時のセキュリティも考慮しているのでネットワークアクセスにはフラグが必要。

deno run --allow-net index.js

Deno Helloworld

interpreter

インタープリタもあるよ

% deno
Deno 1.0.0
exit using ctrl+d or close()
> console.log('Yo')
Yo
undefined
> const yo = 'Yo'
undefined
> yo
Yo
> 

Servest で動かす (React/tsx)

Servest は Deno 向けの http サーバー。
https://servestjs.org/
作者:https://keroxp.me/
Deno 1.0.0 に追従するように Servest も 1.0.0 に対応した。👏

これも Servest の公式サンプルそのままですみません。
React/tsx が普通に動く。
パッケージは https 経由で指定できるので npm install 不要 (node_modules が作られない)。
Router とかも実装されているので実践で使えてしまえそう。

index.tsx
// @deno-types="https://servestjs.org/@v1.0.0/types/react/index.d.ts"
import React from "https://dev.jspm.io/react/index.js";
// @deno-types="https://servestjs.org/@v1.0.0/types/react-dom/server/index.d.ts"
import ReactDOMServer from "https://dev.jspm.io/react-dom/server.js";
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/html; charset=UTF-8",
    }),
    body: ReactDOMServer.renderToString(
      <html>
        <head>
          <meta charSet="utf-8" />
          <title>servest</title>
        </head>
        <body>Hello Servest!</body>
      </html>,
    ),
  });
});
app.listen({ port: 8899 });

そのまま実行

deno run --allow-net index.tsx

スクリーンショット 2020-05-15 8.19.59.png

かんそう

  • JS/TS エンジニアは Node.js からドンドコ移行しそう
  • マスコットかわいいので流行りそう
    • チンアナゴかと思ってた
35
37
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
35
37