Home > Web Front-end > JS Tutorial > What is Deno? What is the difference from Node.js?

What is Deno? What is the difference from Node.js?

青灯夜游
Release: 2021-06-28 11:15:45
forward
3305 people have browsed it

Deno was created to solve some inherent problems of Node, so what is the difference from Node.js? The following article will take you to learn more about Deno and introduce the difference between Deno and Node.js.

What is Deno? What is the difference from Node.js?

[Recommended study: "nodejs Tutorial"]

Ryan Dahl, the author of Node.js, has spent the past year and a half They are all building a new JavaScript running environment Deno to solve some of Node's inherent problems.

But don’t get me wrong, thanks to JavaScript’s huge community ecosystem and scope of use, Node is a very good JavaScript running environment. However, Dahl also admitted that there are some aspects of Node that he should consider more comprehensively, such as: security, module mechanism, dependency management, etc.

In his plan, he did not foresee how large a platform Deno could develop into in a short period of time. Of course, if we go back to 2009, JavaScript was still a weird little language that everyone could make fun of, and it didn’t have as many language features as it does now.

What is Deno and what are its main features?

Deno is a safe TypeScript runtime environment built on the Google V8 engine. The following are some materials for building Deno:

  • Rust (Deno’s core module is written in Rust, and Node’s core module is implemented in C)
  • Tokio (Asynchronous programming implemented in Rust Framework)
  • TypeScript (Deno supports both JavaScript and TypeScript out of the box)
  • V8 (JavaScript runtime produced by Google, mainly used in Chrome and Node)

Next, let’s take a look at what features Deno provides.

Security (Permission Management)

The most important feature of Deno is security.

Compared with Node, Deno uses a sandbox environment to execute code by default, which means that the running environment does not have permission to operate the following modules:

  • File system
  • Network
  • Execute other scripts
  • System environment variables

Let's take a look at how Deno's permission system works.

(async () => {
 const encoder = new TextEncoder();
 const data = encoder.encode('Hello world\n');

 await Deno.writeFile('hello.txt', data);
 await Deno.writeFile('hello2.txt', data);
})();
Copy after login

This script creates two files named hello.txt and hello2.txt respectively, and writes Hello world## in them. #. However, this code runs in a sandbox environment, so it does not have permission to operate the file system.

It is also worth noting that in the above script we use the Deno namespace to operate files, unlike using the

fs module in Node. The Deno namespace provides many basic methods. However, using the Deno namespace will cause our code to lose compatibility with browsers. We will discuss this issue later.

Use the following command to execute the above script:

$ deno run write-hello.ts
Copy after login

After execution, we will receive the following prompt:

Deno requests write access to "/Users/user/folder/hello.txt". Grant? [a/y/n/d (a = allow always, y = allow once, n = deny once, d = deny always)]
Copy after login

In fact, based on the script that creates the file above we will Received two permission prompts from the sandbox environment. However, if we select the

allow always option, we will only be asked once.

If we select

deny, an error of PermissionDenied will be thrown. If we do not write error handling logic, the process will be terminated at this time.

If we execute the script with the following command:

deno run --allow-write write-hello.ts
Copy after login

will create these two files without prompting.

Deno's command line flags for the file system, in addition to

--allow-write, there are also --allow-net/--allow- env/--allow-run are used to enable permissions for the network, system environment variables and operating sub-processes respectively.

Module mechanism

Deno uses the same method as a browser to load modules through URLs. Many people will be a little confused when they see the URL in the import statement on the server for the first time, but for me it is quite easy to understand:

import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Copy after login

What do you think will happen if you introduce the module through the URL? Big deal? The answer is actually quite simple: by using URLs to load modules, Deno can avoid introducing a centralized system like

npm to publish packages. npm has received a lot of complaints recently. . Introducing code through URL allows package authors to maintain and publish their code in their favorite way. No more

package.json

and node_modules. When we start the application, Deno will download all referenced files and cache them locally. Once the references are cached, Deno will not download them again unless we use the

-- relaod

flag to trigger a re-download. There are still a few issues worth discussing:

万一存放引用的站点挂了咋办?

由于没有了一个中心化的包管理站点,那些存放模块的站点可能因为各种各样的原因挂掉。如果在开发甚至生产环境出现这种情况是非常危险滴!

我们在上一节提到,Deno会缓存好已下载的模块。由于缓存是存放在我们的本地磁盘的,Deno的作者建议将这些缓存提交到代码仓库里。这样一来,即使存放引用的站点挂了,开发者们还是可以使用已经下载好的模块(只不过版本是被锁住的啦)。

Deno会把缓存存储在环境变量$DENO_DIR所指定的目录下,如果我们不去设置这个变量,它会指向系统默认的缓存目录。我们可以把$DENO_DIR指定我们的本地仓库,然后把它们提交到版本管理系统中(比如:git

只能使用URL来引用模块吗?

总是敲URL显得有点XX,还好,Deno提供了两种方案来避免我们成为XX。

第一种,你可以在本地文件中将已经引用的模块重新export出来,比如:

export { test, assertEquals } from "https://deno.land/std/testing/mod.ts";
Copy after login

假如上面这个文件叫local-test-utils.ts。现在,如果我们想再次使用test或者assertEquals方法,只需要像下面这样引用它们:

import { test, assertEquals } from './local-test-utils.ts';
Copy after login

看得出来,是不是通过URL来引用它们并不是最重要的啦。

第二种方案,建一个引用映射表,比如像下面这样一个JSON文件:

{
   "imports": {
      "http/": "https://deno.land/std/http/"
   }
}
Copy after login

然后把它像这样import到代码里:

import { serve } from "http/server.ts";
Copy after login

为了让它生效,我们还需要通过--importmap标志位让Deno来引入import映射表:

$ deno run --importmap=import_map.json hello_server.ts
Copy after login

如何进行版本管理

版本管理必须由包作者来支持,这样在client端可以通过在URL中设置版本号来下载:https://unpkg.com/liltest@0.0.5/dist/liltest.js

浏览器兼容性

Deno有计划做到兼容浏览器。从技术上讲,在使用ES module的前提下,我们不需要使用任何类似webpack的打包工具就能在浏览器上运行Deno代码。

不过呢,你可以使用类似Babel这样的工具可以把代码转化成ES5版本的JavaScript,这样可以兼容那些不支持所有最新语言特性的低版本浏览器中,带来的后果就是最终文件里有很多不是必须的冗余代码,增大代码的体积。

结果取决于我们的主要目的是啥。

支持TypeScript开箱即用

不需要任何配置文件就能在Deno中轻易地使用TypeScript。当然咯,你也可以编写纯JavaScript代码,并使用Deno去执行它。

总结

Deno,作为一个新的TypeScript和JavaScript的运行环境,是一个非常有趣的技术项目,并且至今已经稳定发展了一段时间。但是距离在生产环境中去使用它还有比较长的一段路要走。

通过去中心化(或者翻译成分布式?)的机制,把JavaScript生态系统从npm这样中心化的包管理系统中解放了出来。

Dahl希望在这个夏天快结束的时候能够发布1.0版本,所以如果你对Deno未来的新进展感兴趣的话,可以给它个star

最后还有一个日志系统的广告,大家可以去原文查看。

英文原文地址:https://blog.logrocket.com/what-is-deno/

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What is Deno? What is the difference from Node.js?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template