CyanHall.com 创建于 2020-11-12, 上次更新:2021-04-30。
👉  github shields 如果有用请点赞。
Deno 刚出了 1.0! 我们为它制作了一个速查表(Cheatsheet)。 阅读 官方文档了解更多。

1. 安装

    curl -fsSL https://deno.land/x/install/install.sh | sh

# or
brew install deno
# upgrade
deno upgrade

# check version
deno -V # deno 1.0.0
deno -help
  

2. Hello World

    import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World
" });
}
  

3. 运行 Deno 脚本

    deno fmt example.js # format source files
deno run example.js # execute a script

# start the REPL
deno
# evaluate code
deno eval "console.log(30933 + 404)" # 31337
  

4. 引入第三方模块(ES Modules)

    import * as log from "https://deno.land/std/log/mod.ts";
  

5. 允许网络访问

    deno run --allow-net=example.com example.js
# or
deno run --allow-net example.js
  

6. 允许文件访问

    deno run --allow-read example.js
deno run --allow-write example.js
  

7. 命令行参数

    Deno.args.length
Deno.args[0]
  

8. 访问 URL

    const res = await fetch(url);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
  

9. 读取文件

    let file = await Deno.open(filename);
await Deno.copy(file, Deno.stdout);
file.close();
  

10. 一个 echo TCP 服务器

    const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
  Deno.copy(conn, conn);
}
  

11. 执行 wasm 二进制文件

    # Doc: https://deno.land/manual/getting_started/webassembly
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule);
wasmInstance.exports.main()
  

12. 安装一个模块

    deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
# export PATH="$HOME/.deno/bin:$PATH"
file_server . # serves files in the current directory over HTTP. Just like `python3 -m http.server`
  

13. 多线程

    // worker.ts
console.log("hello world");
self.close();

// main.ts
new Worker("./worker.ts", { type: "module" });

// shell
deno run --allow-read main.ts # --allow-read permission is required
  

14. 测试用例

    import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Deno.test("hello world", () => {
  const x = 1 + 2;
  assertEquals(x, 3);
});
  

15. 测试异步代码

    Deno.test("async hello world", async () => {
  const x = 1 + 2;
  // await some async task
  await delay(100);
  if (x !== 3) {
    throw Error("x should be equal to 3");
  }
});
  

16. 运行测试

    deno test # Auto run test match {*_,}test.{js,ts,jsx,tsx} in the current directory (recursively)
# or
deno test my_test.ts
  

1. 安装

    curl -fsSL https://deno.land/x/install/install.sh | sh

# or
brew install deno
# upgrade
deno upgrade

# check version
deno -V # deno 1.0.0
deno -help
  

3. 运行 Deno 脚本

    deno fmt example.js # format source files
deno run example.js # execute a script

# start the REPL
deno
# evaluate code
deno eval "console.log(30933 + 404)" # 31337
  

5. 允许网络访问

    deno run --allow-net=example.com example.js
# or
deno run --allow-net example.js
  

7. 命令行参数

    Deno.args.length
Deno.args[0]
  

9. 读取文件

    let file = await Deno.open(filename);
await Deno.copy(file, Deno.stdout);
file.close();
  

11. 执行 wasm 二进制文件

    # Doc: https://deno.land/manual/getting_started/webassembly
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule);
wasmInstance.exports.main()
  

13. 多线程

    // worker.ts
console.log("hello world");
self.close();

// main.ts
new Worker("./worker.ts", { type: "module" });

// shell
deno run --allow-read main.ts # --allow-read permission is required
  

15. 测试异步代码

    Deno.test("async hello world", async () => {
  const x = 1 + 2;
  // await some async task
  await delay(100);
  if (x !== 3) {
    throw Error("x should be equal to 3");
  }
});
  

2. Hello World

    import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World
" });
}
  

4. 引入第三方模块(ES Modules)

    import * as log from "https://deno.land/std/log/mod.ts";
  

6. 允许文件访问

    deno run --allow-read example.js
deno run --allow-write example.js
  

8. 访问 URL

    const res = await fetch(url);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
  

10. 一个 echo TCP 服务器

    const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
  Deno.copy(conn, conn);
}
  

12. 安装一个模块

    deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
# export PATH="$HOME/.deno/bin:$PATH"
file_server . # serves files in the current directory over HTTP. Just like `python3 -m http.server`
  

14. 测试用例

    import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Deno.test("hello world", () => {
  const x = 1 + 2;
  assertEquals(x, 3);
});
  

16. 运行测试

    deno test # Auto run test match {*_,}test.{js,ts,jsx,tsx} in the current directory (recursively)
# or
deno test my_test.ts
  


Maitained by Cyanhall.com, Copy Rights @ CC BY-NC-SA 4.0