了不起的Deno 下载与入门

下载

下载地址: 下载地址

通过 deno --version 查看当前安装的 Deno 版本

deno-cli

deno-cli 提供了一组集成功能,让你沉浸在 Deno 的专有开发环境中

SUBCOMMANDS:
    bundle          Bandle module and dependencies into single file             将模块和依赖关系绑定到单个文件中
    cache           Cache the dependencies                                      缓存依赖项
    completions     Generate shell completuions                                 生成完整shell
    doc             Show documentation for a module                             显示模块的文档
    eval            Eval script                                                 Eval 脚本
    fmt             Format source files                                         格式化源文件 代码格式化工具
    help            Prints this message or the help of the given subcommand(s)  打印此消息或给定子命令的帮助    
    info            Show info about cache or info related to source file        显示有关缓存或与源文件相关的信息 依赖信息查看器
    install         Install script as an executable                             将脚本安装为可执行文件
    repl            Read Eval Print Loop                                        读取 Eval 打印循环 就是类似 node 的命令行
    run             Run a program given a filename or url to the moudle         运行一个给定模块文件名或 url 的程序
    test            Run tests                                                   运行测试
    types           Print runtime TypeScript declarations                       打印运行时 TypeScript 声明
    upgrade         Upgrade deno executable to given version                    将 deno 可执行文件升级到给定版本

Deno 初体验

第一次运行

$ 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 🦕

如上所示,当运行 deno run https://deno.land/std/examples/welcome.ts 之后, Deno 会先从 https://deno.land/std/examples/welcome.ts URL 地址下载 welcome.ts 文件,该文件的内容是:

console.log("Welcome to Deno 🦕")

当文件下载成功后,Deno 会对 welcome.ts 文件进行编译,即编译成 welcome.ts.js 文件,然后再通过 V8 引擎来执行编译生成的 JavaScript 文件

并且,如果你在命令行重新运行上述命令,则会执行缓存中已生成的文件,并不会再次从网上下载 welcome.ts 文件

之后的运行

$ deno run https://deno.land/std/examples/welcome.ts

Welcome to Deno 🦕

可以介绍 deno info 命令,该命令用于显示有关缓存或源文件相关的信息:

$ deno info

DENO_DIR location: "/Users/fer/Library/Caches/deno"
Remote modules cache: "/Users/fer/Library/Caches/deno/deps"
TypeScript compiler cache: "Users/fer/Library/Caches/deno/gen

TypeScript compiler cache 这是 TypeScript 编译器缓存的目录,在 examples 目录下找到了 welcome.ts.js 文件

// welcome.ts.js
"use strict"
console.log("welcome to Deno 🦕");
//# sourceMappingURL=file:///Users/fer/Library/Caches/deno/gen/https/deno.land/std/examples/welcome.ts.js.map

但是这里有一个问题,就是当我们修改缓存的内容之后,运行命令会运行我们修改过的内容,而不是本来的内容

welcome.ts.js 中添加 console.log("Hello Semlinker, from Cache");

// welcome.ts.js
"use strict"
console.log("Hello Semlinker, from Cache");
console.log("welcome to Deno 🦕");
//# sourceMappingURL=file:///Users/fer/Library/Caches/deno/gen/https/deno.land/std/examples/welcome.ts.js.map

然后重新执行命令:

$ deno run https://deno.land/std/examples/welcome.ts

Hello Semlinker, from Cache
Welcome to Deno 🦕

这时我们可以通过 deno run --reload 来强制刷新缓存,重新编译 TypeScript 文件

HTTP Server

import { serve } from "https://deno.land/std@v0.50.0/http/server.ts";

const PORT = 8080;
const s = serve({ port: PORT });

console.log(`Listening on @ http://localhost:${PORT}`);

for await (const req of s) {
    req.respond({ body: "Hello Semlinker"});
}

上面代码中,导入了 Deno 标准库 http 模块中的 serve 函数,然后适用该函数快速创建 HTTP 服务器,该函数定义如下:

// std/http/server.ts

export function serve(addr: string | HTTPOptions): Server {
  if (typeof addr === 'string') {
    const [hostname, port] = addr.split(":");
    addr = { hostname, port: Number(port) };
  }

  const listener = listen(addr);
  return new Server(listener);
}

在使用语句 deno run --allow-net ./xx.ts 运行,服务就会跑在 8080 端口上了

最后更新于