【Deno】初识Deno

5月13号 Deno 发布了 1.0 版本,Deno 的发布引发了不少人关注。前端

官网描述 Deno 是一个安全的 JavaScript 和 TypeScript 的运行时。node

A secure runtime for JavaScript and TypeScript. ---https://deno.land/git

Deno 的做者 Ryan Dahl (ry) 就是 Node.js 的做者,由于 Node.js 存在设计上存在缺陷,并且 Node.js 拥有大量的用户使得 Node.js 的发展变得困难、缓慢,因此 Ryan Dahl 选择离开 Node.js 开发 Deno。github

Deno 与 Node.js 的区别

异步操做

在 Node.js 设计之初,JavaScript 尚未 Promise、async/await 的概念。Node.js 经过回调函数的方式实现异步操做。这就致使了在 Node.js 中存在回调函数和 Promise 两种写法。shell

而 Deno 全部的异步操做将会返回一个 Promise。json

模块

Node.js 使用与 ES 模块不兼容的 CommonJS,Deno 使用的则是浏览器一致的 ES 模块。api

外部模块

Node.js 使用 NPM 管理外部模块,node_modules 极其复杂。浏览器

Deno 经过 url 连接外部模块,能够使用绝对路径或相对路径导入模块,所以外部模块能够存放在任意系统,不须要集中存放在相似 NPM 的模块管理中心。缓存

例如:安全

import { foo } from "https://foo.com/foo.ts";
import { foo } from "./foo.ts";
复制代码

须要注意的是在 Deno 中使用外部模块不能省略后缀。

Deno 在首次运行时会将外部模块下载到本地缓存。

安全

Node.js 没有任何安全性可言,所以是否是会传出某个 NPM 包中存在恶意代码的消息。

而 Deno 在执行时须要开发者进行对应操做的受权。

# 容许全部受权
-A, --allow-all Allow all permissions  # 容许读取环境变量  --allow-env Allow environment access  # 容许高精度时间测量  --allow-hrtime Allow high resolution time measurement  # 容许网络通讯,支持指定域名  --allow-net=<allow-net> Allow network access  # 容许加载插件  --allow-plugin Allow loading plugins  # 容许文件读操做,能够指定文件  --allow-read=<allow-read> Allow file system read access  # 容许运行子进程  --allow-run Allow running subprocesses  # 容许文件写操做,能够指定文件  --allow-write=<allow-write> Allow file system write access 复制代码

支持 TypeScript

Deno 不须要额外配置,默认就支持 TypeScript。Deno 会经过文件后缀名进行判断,.ts 文件会先经过 TS 编译器转成 JS,.js 文件则会传入 V8 引擎运行。

安装 Deno

在 Deno 的官网中提供了各个系统的安装方法,这里简单搬运一下。

Using Shell (macOS, Linux):

curl -fsSL https://deno.land/x/install/install.sh | sh
复制代码

Using PowerShell (Windows):

iwr https://deno.land/x/install/install.ps1 -useb | iex
复制代码

Using Cargo (Windows, macOS, Linux):

cargo install deno
复制代码

Using Homebrew (macOS):

brew install deno
复制代码

Using Chocolatey (Windows):

choco install deno
复制代码

Using Scoop (Windows):

scoop install deno
复制代码

关于 Deno 更多安装选项,例如指定 Deno 版本安装等能够到 https://github.com/denoland/deno_install 查看。

Deno 的运行参数及子命令

经过执行 deno -h 能够查看 Deno 的帮助信息。

# deno 版本
deno 0.42.0 A secure JavaScript and TypeScript runtime  # 文档地址 Docs: https://deno.land/std/manual.md # 标准库及第三方模块的地址 Modules: https://deno.land/std/ https://deno.land/x/ # bug 反馈地址 Bugs: https://github.com/denoland/deno/issues  # 无需参数就可启动 REPL 环境 To start the REPL, supply no arguments:  deno  # 执行脚本 To execute a script:  deno run https://deno.land/std/examples/welcome.ts  deno https://deno.land/std/examples/welcome.ts  # 在 shell 中执行代码 To evaluate code in the shell:  deno eval "console.log(30933 + 404)"  # 运行 deno help run 查看 run 命令的特殊标记 Run 'deno help run' for 'run'-specific flags.  # 用法 deno [参数] [子命令] USAGE:  deno [OPTIONS] [SUBCOMMAND]  OPTIONS:  # 查看帮助信息  -h, --help Prints help information  # 设置日志级别,可选值 [debug, info]  -L, --log-level <log-level> Set log level [possible values: debug, info]  # 禁止输出  -q, --quiet Suppress diagnostic output  # 查看版本信息  -V, --version Prints version information  SUBCOMMANDS:  # 将模块和依赖打包成单文件  bundle Bundle module and dependencies into single file  # 缓存依赖  cache Cache the dependencies  completions Generate shell completions  # 显示文档  doc Show documentation for a module  # 执行脚本  eval Eval script  # 格式化源码  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 环境  repl Read Eval Print Loop  # 运行脚本  run Run a program given a filename or url to the module  # 运行测试  test Run tests  # 打印运行时 TS 类型声明  types Print runtime TypeScript declarations  # 升级 Deno 到最新版本  upgrade Upgrade deno executable to newest version  # 环境变量 ENVIRONMENT VARIABLES:  # 设置 deno 的基础目录,默认在 $HOME/.deno  DENO_DIR Set deno's base directory (defaults to $HOME/.deno)  # deno install 输出的目录,默认在 $HOME/.deno/bin  DENO_INSTALL_ROOT Set deno install's output directory  (defaults to $HOME/.deno/bin)  # 关闭颜色  NO_COLOR Set to disable color  # http 代理  HTTP_PROXY Proxy address for HTTP requests  (module downloads, fetch)  # https 代理  HTTPS_PROXY Same but for HTTPS 复制代码

案例

开启 http 服务

// demo1.ts
import { serve } from 'https://deno.land/std@0.50.0/http/server.ts';  for await (const req of serve({ hostname: '0.0.0.0', port: 8000 })) {  req.respond({ body: 'Hello Deno. \n' }); } 复制代码

运行

deno run demo1.ts
复制代码
Compile file:///Users/test/demo1.ts
Download https://deno.land/std@0.50.0/http/server.ts ... error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag  at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)  at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)  at Object.listen ($deno$/ops/net.ts:51:10)  at listen ($deno$/net.ts:164:18)  at serve (server.ts:261:20)  at demo1.ts:3:25 复制代码

首先会编译代码,编译完成后下载外部模块到本地,下载完成就能够执行代码。执行时会检查所需权限,若是没有受权则会报 error: Uncaught PermissionDenied

加上 --allow-net 再执行便可。

总结

相比于 Node.js,Deno 在使用上更加简单,大部分 api 与浏览器一致,做为前端码农应该会更加容易接受 Deno。在未来咱们能够根据项目来选择 Node.js 或者 Deno,这对于咱们来讲应该是一件好事。

短时间内 Deno 还不能用于生产环境,经过各大框架对 TypeScript 的支持能够知道 TypeScript 对于前端来讲已经愈来愈重要了,因此仍是抓紧学习 TypeScript 吧。


以上内容均是我的理解,若是有讲的不对的地方,还请各位大佬指点。

若是以为内容还不错的话,但愿小伙伴能够帮忙点赞转发,给更多的同窗看到,感谢感谢!

若是你喜欢个人文章,还能够关注个人公众号【前端develop】

前端develop
前端develop
相关文章
相关标签/搜索