node deno_Deno手册:带有代码示例的TypeScript运行时教程

2023-11-08

node deno

I explore new projects every week, and it’s rare that one grabs my attention as much as Deno did.

我每周都会探索新的项目,很少有人像Deno那样吸引我的注意力。

In this post I want to get you up to speed with Deno quickly. We'll compare it with Node.js, and build your first REST API with it.

在这篇文章中,我想让您快速了解Deno。 我们将其与Node.js进行比较,并以此构建您的第一个REST API。

目录 (Table of contents)

And note: You can get a PDF/ePub/Mobi version of this Deno Handbook here.

请注意: 您可以在此处获取此Deno手册的PDF / ePub / Mobi版本

什么是德诺? (What is Deno?)

If you are familiar with Node.js, the popular server-side JavaScript ecosystem, then Deno is just like Node. Except deeply improved in many ways.

如果您熟悉流行的服务器端JavaScript生态系统Node.js,那么Deno就像Node。 除了在很多方面都得到了深深的改善。

Let’s start from a quick list of the features I like the most about Deno:

让我们从我最喜欢的Deno快速功能列表开始:

  • It is based on modern features of the JavaScript language

    它基于JavaScript语言的现代功能
  • It has an extensive standard library

    它具有广泛的标准库
  • It has TypeScript at its core, which brings a huge advantage in many different ways, including a first-class TypeScript support (you don’t have to separately compile TypeScript, it’s automatically done by Deno)

    它以TypeScript为核心,以许多不同的方式带来了巨大的优势,包括一流的TypeScript支持(您不必单独编译TypeScript,它由Deno自动完成)
  • It embraces ES modules

    它包含ES模块

  • It has no package manager

    它没有包管理器
  • It has a first-class await

    它有一流的await

  • It has a built-in testing facility

    它具有内置的测试设施
  • It aims to be browser-compatible as much as it can, for example by providing a built-in fetch and the global window object

    它旨在尽可能地与浏览器兼容,例如,通过提供内置的fetch和全局window对象

We’ll explore all of those features in this guide.

我们将在本指南中探索所有这些功能。

After you use Deno and learn to appreciate its features, Node.js will look like something old.

在使用Deno并了解了其功能之后,Node.js看起来会很

Especially because the Node.js API is callback-based, as it was written way before promises and async/await. There’s no change available for that in Node, because such a change would be monumental. So we’re stuck with callbacks or with promisifying API calls.

特别是因为Node.js API是基于回调的,因此它是在promise和async / await之前编写的。 Node中没有可用的更改,因为这样的更改将是巨大的。 因此,我们陷入了回调或大量API调用的困境。

Node.js is awesome and will continue to be the de facto standard in the JavaScript world. But I think we’ll gradually see Deno get adopted more and more because of its first-class TypeScript support and modern standard library.

Node.js 非常棒 ,并将继续成为JavaScript世界中的事实上的标准。 但是我认为我们将逐渐看到Deno由于其一流的TypeScript支持和现代标准库而越来越被采用。

Deno can afford to have everything written with modern technologies, since there’s no backward compatibility to maintain. Of course there’s no guarantee that in a decade the same will happen to Deno and a new technology will emerge, but this is the reality at the moment.

由于没有向后兼容性,因此Deno可以用现代技术编写所有内容。 当然,我们无法保证十年之内Deno也会发生同样的事情,并且会出现一项新技术,但这是目前的现实。

为什么是德诺? 为什么现在? (Why Deno? Why now?)

Deno was announced almost 2 years ago by the original creator of Node.js, Ryan Dahl, at JSConf EU. Watch the YouTube video of the talk, it’s very interesting and it’s a mandatory watch if you are involved in Node.js and JavaScript in general.

大约2年前,Node.js的原始创建者Ryan Dahl在JSConf EU上宣布了Deno。 观看YouTube演讲视频 ,这非常有趣,如果您通常参与Node.js和JavaScript,那么这是必看的。

Every project manager must make decisions. Ryan regretted some early decisions in Node. Also, technology evolves, and today JavaScript is a totally different language than what it was back in 2009 when Node started. Think about the modern ES6/2016/2017 features, and so on.

每个项目经理都必须做出决定。 Ryan对Node中的一些早期决定感到遗憾。 此外,技术也在不断发展,如今JavaScript与2009年Node创立时的语言已经完全不同。 考虑一下现代的ES6 / 2016/2017功能,等等。

So he started a new project to create some sort of second wave of JavaScript-powered server side apps.

因此,他开始了一个新项目,以创建第二波基于JavaScript的服务器端应用程序。

The reason I am writing this guide now and not back then is because technologies need a lot of time to mature. And we have finally reached Deno 1.0 (1.0 should be released on May 13, 2020), the first release of Deno officially declared stable.

我现在而不是那时写本指南的原因是,技术需要大量时间才能成熟。 我们终于达到了Deno 1.0 (1.0应该在2020年5月13日发布),这是Deno的第一个正式宣布稳定的版本。

That’s might seem to be just a number, but 1.0 means there will not be major breaking changes until Deno 2.0. This is a big deal when you dive into a new technology - you don’t want to learn something and then have it change too fast.

这似乎只是一个数字,但是1.0意味着直到Deno 2.0才不会有重大的重大变化。 当您深入研究新技术时,这很重要-您不想学习某些东西,然后让它改变得太快。

您应该学习Deno吗? (Should you learn Deno?)

That’s a big question.

这是一个大问题。

Learning something new such as Deno is a big effort. My suggestion is that if you are starting out now with server-side JS and you don’t know Node yet, and have never written any TypeScript, I’d start with Node.

学习诸如Deno之类的新东西是一项巨大的努力。 我的建议是,如果您现在开始使用服务器端JS,并且您还不了解Node,并且从未编写过任何TypeScript,那么我将从Node开始。

No one was ever fired for choosing Node.js (paraphrasing a common quote).

没有人因为选择Node.js而被解雇(用一个共同的说法解释)。

But if you love TypeScript, don’t depend on a gazillion npm packages in your projects and you want to use await anywhere, hey Deno might be what you’re looking for.

但是,如果您喜欢TypeScript,则不要依赖项目中庞大的npm软件包,而想要await任何地方使用await ,那么Deno可能就是您想要的。

它将取代Node.js吗? (Will it replace Node.js?)

No. Node.js is a giant, well established, incredibly well-supported technology that is going to stay around for decades.

不会。Node.js是一项庞大的,完善的,获得了难以置信的良好支持的技术,它将持续数十年。

一流的TypeScript支持 (First-class TypeScript support)

Deno is written in Rust and TypeScript, two of the languages that are really growing fast today.

Deno用Rust和TypeScript编写,这两种语言今天正在Swift发展。

In particular, being written in TypeScript means we get a lot of the benefits of TypeScript even if we might choose to write our code in plain JavaScript.

特别是,使用TypeScript编写意味着即使我们可能选择使用纯JavaScript编写代码,我们也可以获得TypeScript的很多好处。

And running TypeScript code with Deno does not require a compilation step - Deno does that automatically for you.

使用Deno运行TypeScript代码不需要编译步骤-Deno会自动为您完成此步骤。

You are not forced to write in TypeScript, but the fact the core of Deno is written in TypeScript is huge.

您没有被迫使用TypeScript编写代码,但是Deno的核心是用TypeScript编写的事实是巨大的。

First, an increasingly large percentage of JavaScript programmers love TypeScript.

首先,越来越多JavaScript程序员喜欢TypeScript。

Second, the tools you use can infer many information about software written in TypeScript, like Deno.

其次,您使用的工具可以推断出许多有关用TypeScript编写的软件的信息,例如Deno。

This means that when we code in VS Code, for example (which of course has a tight integration with TypeScript since both are developed at MicroSoft), we can get benefits like type checking as we write our code, and advanced IntelliSense features. In other words the editor can help us in a deeply useful way.

这意味着,例如,当我们使用VS Code进行编码时(由于两者都是在MicroSoft上开发的,因此与TypeScript紧密集成),我们可以在编写代码时获得类型检查和高级IntelliSense功能等好处。 换句话说,编辑器可以以非常有用的方式帮助我们。

与Node.js的异同 (Similarities and differences with Node.js)

Since Deno is basically a Node.js replacement, it’s useful to compare the two directly.

由于Deno基本上是Node.js的替代品,因此直接比较两者非常有用。

Similarities:

相似之处:

Differences:

差异:

  • Node is written in C++ and JavaScript. Deno is written in Rust and TypeScript.

    Node用C ++和JavaScript编写。 Deno用Rust和TypeScript编写。
  • Node has an official package manager called npm. Deno does not, and instead lets you import any ES Module from URLs.

    Node有一个名为npm的官方软件包管理器。 Deno不会,而是让您从URL导入任何ES模块。

  • Node uses the CommonJS syntax for importing pacakges. Deno uses ES Modules, the official way.

    Node使用CommonJS语法导入pacakges。 Deno使用官方的ES模块。
  • Deno uses modern ECMAScript features in all its API and standard library, while Node.js uses a callbacks-based standard library and has no plans to upgrade it.

    Deno在其所有API和标准库中都使用了现代ECMAScript功能,而Node.js使用了基于回调的标准库,并且没有计划对其进行升级。
  • Deno offers a sandbox security layer through permissions. A program can only access the permissions set to the executable as flags by the user. A Node.js program can access anything the user can access.

    Deno通过权限提供了一个沙箱安全层。 程序只能访问由用户设置为可执行文件的权限作为标志。 Node.js程序可以访问用户可以访问的任何内容。
  • Deno has for a long time envisioned the possibility of compiling a program into an executable that you can run without external dependencies, like Go, but it’s still not a thing yet. That’d be a game changer.

    Deno长期以来一直在考虑将程序编译为可执行文件的可能性,而该可执行文件可以在没有外部依赖项的情况下运行,例如Go,但这还不是一件容易的事 。 那将改变游戏规则。

没有包裹经理 (No package manager)

Having no package manager and having to rely on URLs to host and import packages has pros and cons. I really like the pros: it’s very flexible, and we can create packages without publishing them on a repository like npm.

没有程序包管理器并且必须依靠URL来承载和导入程序包是有利有弊。 我真的很喜欢专家:它非常灵活,我们可以创建软件包而无需在npm这样的存储库中发布它们。

I think that some sort of package manager will emerge, but nothing official is out yet.

我认为某种软件包管理器将会出现,但是还没有官方的消息。

The Deno website provides code hosting (and thus distribution through URLs) to 3rd party packages: https://deno.land/x/

Deno网站为第三方软件包提供代码托管(并因此通过URL分发): https : //deno.land/x/

安装Deno (Install Deno)

Enough talk! Let’s install Deno.

聊够了! 让我们安装Deno。

The easiest way is to use Homebrew:

最简单的方法是使用Homebrew

brew install deno

Once this is done, you will have access to the deno command. Here’s the help that you can get using deno --help:

完成此操作后,您将可以访问deno命令。 这是您可以使用deno --help获得的帮助:

flavio@mbp~> deno --help
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/
Bugs: https://github.com/denoland/deno/issues

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

To evaluate code in the shell:
  deno eval "console.log(30933 + 404)"

Run 'deno help run' for 'run'-specific flags.

USAGE:
    deno [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -h, --help
            Prints help information

    -L, --log-level <log-level>
            Set log level [possible values: debug, info]

    -q, --quiet
            Suppress diagnostic output
            By default, subcommands print human-readable diagnostic messages to stderr.
            If the flag is set, restrict these messages to errors.
    -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           Read Eval Print Loop
    run            Run a program given a filename or url to the module
    test           Run tests
    types          Print runtime TypeScript declarations
    upgrade        Upgrade deno executable to newest version

ENVIRONMENT VARIABLES:
    DENO_DIR             Set deno's base directory (defaults to $HOME/.deno)
    DENO_INSTALL_ROOT    Set deno install's output directory
                         (defaults to $HOME/.deno/bin)
    NO_COLOR             Set to disable color
    HTTP_PROXY           Proxy address for HTTP requests
                         (module downloads, fetch)
    HTTPS_PROXY          Same but for HTTPS

Deno命令 (The Deno commands)

Note the SUBCOMMANDS section in the help, that lists all the commands we can run. What subcommands do we have?

请注意帮助中的“ SUBCOMMANDS部分,其中列出了我们可以运行的所有命令。 我们有哪些子命令?

  • bundle bundle module and dependencies of a project into single file

    bundle束模块和项目的依赖关系到单个文件

  • cache cache the dependencies

    cache依赖项

  • completions generate shell completions

    completions生成壳补全

  • doc show documentation for a module

    doc显示模块的文档

  • eval to evaluate a piece of code, e.g. deno eval "console.log(1 + 2)"

    eval评估一段代码,例如deno eval "console.log(1 + 2)"

  • fmt a built-in code formatter (similar to gofmt in Go)

    fmt内置的代码格式化程序(类似于Go中的gofmt )

  • help prints this message or the help of the given subcommand(s)

    help打印此消息或给定子命令的帮助

  • info show info about cache or info related to source file

    info显示有关缓存的信息或与源文件相关的信息

  • install install script as an executable

    install安装脚本安装为可执行文件

  • repl Read-Eval-Print-Loop (the default)

    repl Read-Eval-Print-Loop(默认值)

  • run run a program given a filename or url to the module

    run给模块指定文件名或URL的程序

  • test run tests

    test运行测试

  • types print runtime TypeScript declarations

    types打印运行时TypeScript声明

  • upgrade upgrade deno to the newest version

    upgrade升级deno到最新版本

You can run deno <subcommand> help to get specific additional documentation for the command, for example deno run --help.

您可以运行deno <subcommand> help来获取该命令的特定其他文档,例如deno run --help

As the help says, we can use this command to start a REPL (Read-Execute-Print-Loop) using deno without any other option.

如帮助所述,我们可以使用此命令使用deno来启动REPL(Read-Execute-Print-Loop),而无需任何其他选择。

This is the same as running deno repl.

这与运行deno repl相同。

A more common way you’ll use this command is to execute a Deno app contained in a TypeScript file.

您将使用此命令的更常见方式是执行TypeScript文件中包含的Deno应用程序。

You can run both TypeScript (.ts) files, or JavaScript (.js) files.

您可以同时运行TypeScript( .ts )文件或JavaScript( .js )文件。

If you are unfamiliar with TypeScript, don’t worry: Deno is written in TypeScript, buf you can write your “client” applications in JavaScript.

如果您不熟悉TypeScript,请不要担心:Deno是用TypeScript编写的,但是您可以使用JavaScript编写“客户端”应用程序。

My TypeScript tutorial will help you get up and running quickly with TypeScript if you want.

如果需要,我的TypeScript教程将帮助您快速入门并使用TypeScript。

您的第一个Deno应用 (Your first Deno app)

Let’s run a Deno app for the first time.

让我们第一次运行Deno应用程序。

What I find pretty amazing is that you don’t even have to write a single line - you can run a command from any URL.

我感到非常惊奇的是,您甚至不必写一行-您可以从任何URL运行命令。

Deno downloads the program, compiles it and then runs it:

Deno下载程序,对其进行编译,然后运行:

Of course running arbitrary code from the Internet is not a practice I'd generally recommend. In this case we are running it from the Deno official site, plus Deno has a sandbox that prevents programs to do anything you don’t want to allow. More on this later.

当然,我一般不建议 从Internet运行任意代码 在这种情况下,我们从Deno官方站点运行它,另外Deno还有一个沙箱,可以阻止程序执行您不希望做的任何事情。 稍后再详细介绍。

This program is very simple, just a console.log() call:

该程序非常简单,只需进行console.log()调用即可:

console.log('Welcome to Deno 									
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

node deno_Deno手册:带有代码示例的TypeScript运行时教程 的相关文章

随机推荐

  • 这里是哪里错显示[Error] ‘else‘ without a previous ‘if‘?

    include
  • (c语言 )输入10个学生5门课的成绩,分别用函数求:每个学生平均分;每门课的平均分;

    c语言 输入10个学生5门课的成绩 分别用函数求 每个学生平均分 每门课的平均分 include
  • 新版Spring Boot(9)- Spring Boot 整合 Web 开发(5)

    1 路径映射 不需要每个Controller都加路径映射 Configuration public class WebMvcConfig implements WebMvcConfigurer 页面只是简单的返回 不需要返回数据 param
  • WPF Image 旋转图片方法

    WPF在对图片进行旋转时 可以使用Image RenderTransform 设置它的初始旋转角度Angle 默认是0 在旋转照片图像时 还需要设置图片的旋转中心 CenterX CenterY 一般都设置它的宽度一半 高度一半 先看一下效
  • php按钮添加post请求,PHP发送POST请求的常用方式

    PHP发送POST请求我们使用的是curl来操作了 下面来看看一些常用的post数据的例子 具体的一起和111cn小编来看看吧 在PHP开发的过程中经常需要发送POST请求 POST相比GET要安全很多 而且传输的数据量也较大 下面PHP程
  • uboot启动内核的相关命令详解——boot、bootm

    1 boot和bootm命令的联系 当我们进入uboot的命令终端后 可以利用boot和bootm来启动内核 但是命令的使用方式有区别 直接输入boot命令就可以启动内核 如果使用bootm命令 后面还需要传入内核在DDR中的地址 1 bo
  • css内容达到最底部但滚动条没有滚动到底部

    也是犯了一个傻狗一样的错误 滚动条样式是直接复制的蓝湖的代码 有个高度 然后就出现了这样的bug 看了好久一直以为是布局或者overflow的问题 最后发现是因为我给这个滚动条加了个高度 我也是傻狗一样的 滚动条高度是通过内容计算出来的 怎
  • QTabWidget 竖向 QTabBar横向

    参考文件引用自http blog csdn net skyztttt article details 52448992 你的评论 是我的动力 正常设置QTabWidget gt setTabPosition QTabWidget West
  • 2023年第一届山东省职业技能大赛“网络安全”赛项B模块任务书

    2023年第一届山东省职业技能大赛 网络安全 赛项B模块任务书 模块B 网络安全事件响应 数字取证调查和应用程序安全 1竞赛项目简介 1 1 介绍 1 2 所需的设备 机械 装置和材料 1 3 评分方案 1 4 项目和任务的描述 2工作任务
  • React-JSX

    react JSX 这种在js里面写标签的语法叫JSX 所谓的JSX其实就是JS的对象 JSX就是Javascript和XML结合的一种格式 React发明了JSX 利用HTML语法来创建虚拟DOM 当遇到 lt JSX就当HTML解析 遇
  • 如何以管理员的身份唤起powershell

    使用运行窗口打开带管理员权限的PowerShell 1 按下组合键Windows R以打开运行窗口 输入powershell然后按下回车键 2 Windows PowerShell会以当前用户的权限去执行 3 如果你想要从普通模式转至管理员
  • 【Spring Boot 初识丨九丨 外部化配置(三)】类型安全配置属性

    上一篇讲了 Spring Boot 的外部化配置的加载顺序及一些简单的属性说明 本篇来讲一讲 外部化配置类型安全属性 Spring Boot 初识 Spring Boot 初识丨一 入门实战 Spring Boot 初识丨二 maven S
  • DPB详解

    解码图像缓存器 decoded picture buffer DPB 用于存放解码图像 DPB中既存在参考图像也存在非参考图像 那些不用于参考的图像输出后会被移除出DPB DPB的容量由SPS中的sps max dec pic buffer
  • 关于CNN中的maps

    1 feature maps 在每个卷积层 数据都是以三维形式存在的 你可以把它看成许多个二维图片叠在一起 其中每一个称为一个feature map 在输入层 如果是灰度图片 那就只有一个feature map 如果是彩色图片 一般就是3个
  • ESP32开发路程LVGL篇(三)——显示图片

    目录 显示图片 在线转换图片 图片加入项目 主函数代码 参考 LVGL 图片 显示图片 本文利用的方式 通过工具将图片转化为 c文件 写入单片机程序并进行烧录 这种方式实现起来较为简单 但是由于硬件限制 图片的大小会受限 且占用内存 可以用
  • Mysql数据库10万条数据多表联合查询速度过慢解决方案

    点我 查看原文 问题描述 今天在写项目时发现之前好好的查询接口突然挂了 检查后发现原来是有人往数据里新增了10万条数据 以至于Mysql语句执行的特别慢就不行了 原因 原因也简单这个接口原本是一张组织机构表关联区域表 组织标准类型表 币种表
  • xxl-job任务管理平台的配置与使用

    xxl job任务管理平台的配置 是否启用job executor 如果设置为false 则不初始化 job executor enable true web port server port 8083 调度中心部署根地址 选填 如调度中心
  • Linux 粘滞位 suid sgid

    粘滞位 o t 针对目录赋权 目录中创建的文件只有创建者才可以删除 命令 chmod o t 目录名 删除权限用减即可 sgid g s 针对目录建立权限 在该目录中建立的文件所属组继承父目录的属组 命令 chmod g s 目录名 删除权
  • java实体类非空判断@NotEmpty、@NotNull等注解无效解决

    1 引入Spring Hibernate Validator的依赖 此Hibernate 非Hibernate ORM框架的Hibernate
  • node deno_Deno手册:带有代码示例的TypeScript运行时教程

    node deno I explore new projects every week and it s rare that one grabs my attention as much as Deno did 我每周都会探索新的项目 很少
Powered by Hwhale