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
在 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 複製代碼
Deno 不須要額外配置,默認就支持 TypeScript。Deno 會經過文件後綴名進行判斷,.ts
文件會先經過 TS 編譯器轉成 JS,.js
文件則會傳入 V8
引擎運行。
在 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 -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】