前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hello, Deno

Hello, Deno

作者头像
后端云
发布2022-11-25 17:04:33
4880
发布2022-11-25 17:04:33
举报
文章被收录于专栏:后端云后端云

Deno 是什么

现代JS/TS的运行时。

Deno的创始人和Node的创始人是同一个人。把Node的前两个字母和后两个字母调换了下,destroy node,要用Rust重写js/ts的运行时,来解决设计Node.js之初的缺陷,并要在Deno中解决这些问题。

Deno 和 Node 的对比

对比

Node

Deno

开发语言

C++

Rust

工具链

未集成,需要调用外部工具链npm,webpack,babel,typescript comoile、eslint、prettier。。。

部分工具链集成在Deno中

运行时

是JS的运行时

JS,TS,WebAssembly的运行时

安全控制

有安全控制,需要获得某个权限要显式打开权限

异步

Callback被称为回調地獄。很多Node原生API都是使用 CallBack Function 而不是 Promise

原生使用Promise

异步运行时

libuv

Tokio

JS模块化规范

使用CommonJS,与 ES 模块不兼容

只支持 ES 模块,跟浏览器的模块加载规则一致,不支持 CommonJS 模块

模块系统

中心化模块系统npm

去中心化模块系统,可以从任何地方加载模块。这点类似Golang

install

Deno是Rust的模块,可以用cargo安装。也可以用脚本安装。

代码语言:javascript
复制
# 三种安装方式:Rust包安装:cargo install deno --locked
win: iwr https://deno.land/install.ps1 -useb | iex
linux/Mac: curl -fsSL https://deno.land/install.sh | sh

run

hello world

代码语言:javascript
复制
PS C:\Users\hanwei> deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using latest version (0.149.0) for https://deno.land/std/examples/welcome.ts
Download https://deno.land/std@0.149.0/examples/welcome.ts
Welcome to Deno! deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using latest version (0.149.0) for https://deno.land/std/examples/welcome.ts
Download https://deno.land/std@0.149.0/examples/welcome.ts
Welcome to Deno!

发送 HTTP 请求

代码语言:javascript
复制
PS C:\Users\hanwei> deno run --allow-net=example.com https://deno.land/std/examples/curl.ts https://example.com<!doctype html><html><head>
    <title>Example Domain</title>
    ...

读取文件

代码语言:javascript
复制
PS C:\Users\hanwei> deno run --allow-read https://deno.land/std@/examples/cat.ts "C:\Windows\System32\Drivers\etc\hosts"Download https://deno.land/std@/examples/cat.ts
error: Module not found "https://deno.land/std@/examples/cat.ts".PS C:\Users\hanwei> deno run --allow-read https://deno.land/std/examples/cat.ts "C:\Windows\System32\Drivers\etc\hosts"# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.## This file contains the mappings of IP addresses to host names. Each# entry should be kept on an individual line. The IP address should# be placed in the first column followed by the corresponding host name.# The IP address and the host name should be separated by at least one# space.## Additionally, comments (such as these) may be inserted on individual# lines or following the machine name denoted by a '#' symbol.# localhost name resolution is handled within DNS itself.#       127.0.0.1       localhost#       ::1             localhost

TCP服务器

这是一个服务器的例子,它接受端口 8080 上的连接,并向客户机返回它发送的任何内容。linux用nc测试,windows可用telnet测试。nc localhost 8080 或者 telnet localhost 8080

代码语言:javascript
复制
PS C:\Users\hanwei> deno run --allow-net https://deno.land/std/examples/echo_server.ts
Listening on http://localhost:8080
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 后端云 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Deno 是什么
  • Deno 和 Node 的对比
  • install
  • run
    • hello world
      • 发送 HTTP 请求
        • 读取文件
          • TCP服务器
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档