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(使用前将#替换为@)