m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

denoを使ってみた

deno.land

denoって何?

ja.wikipedia.org

NodeJSの改良版みたいなイメージかも・・・

インストール

brew install deno

バージョン確認

deno -V
deno 1.20.6
%

サンプルサプリ作成

簡易Webサーバ

server.js

import { serve } from "https://deno.land/std@0.89.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\n" });
}

実行

deno run --allow-net server.js

実行する際はパーミッションつけないと毎回聞いてくる

ちょっとルーティングがある版

import { Application } from "https://deno.land/x/abc@v1.3.2/mod.ts";
import { renderFile } from "https://deno.land/x/dejs@0.10.1/mod.ts";
import { readAll } from "https://deno.land/std@0.110.0/io/util.ts";

const app = new Application();

console.log("http://localhost:8080/");

app.renderer = {
  async render(name, data) {
    return renderFile(name, data).then(readAll);
  },
};

app
  .get("/", (c) => {
    return c.render("index.html");
  })
  .get("/callback", (c) => {
    console.log("-----------------");
    console.log(c);
    return "aaaa";
  })
  .get("/login", (c) => {
    return c.render("login.html");
  })
  .get("/logout", (c) => {
    return c.redirect("/login");
  })
  .start({ port: 8080 });

パッケージについて

なんか実行した際、新規のものはダウンロードされているみたいだけどどこになるのか? ↓

% deno info
DENO_DIR location: /Users/ユーザー名/Library/Caches/deno
Remote modules cache: /Users/ユーザー名/Library/Caches/deno/deps
Emitted modules cache: /Users/ユーザー名/Library/Caches/deno/gen
Language server registries cache: /Users/ユーザー名/Library/Caches/deno/registries
Origin storage: /Users/ユーザー名/Library/Caches/deno/location_data
%

所感

まだ触り始めたばかりでなんとも言えない感じ node_modulesがローカルにできなくなったことが良いか悪いかによるのかな まだ、npmやyarnを使うことが多いため、すぐに移行することはないけど少しずつwatchしていく感じ