Table of Contents
What is Deno and what are its main features?
Security (Permission Management)
Module mechanism
万一存放引用的站点挂了咋办?
只能使用URL来引用模块吗?
如何进行版本管理
浏览器兼容性
支持TypeScript开箱即用
总结
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?

Jun 24, 2021 am 11:28 AM
node

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

How to delete node in nvm How to delete node in nvm Dec 29, 2022 am 10:07 AM

How to delete node with nvm: 1. Download "nvm-setup.zip" and install it on the C drive; 2. Configure environment variables and check the version number through the "nvm -v" command; 3. Use the "nvm install" command Install node; 4. Delete the installed node through the "nvm uninstall" command.

How to do Docker mirroring of Node service? Detailed explanation of extreme optimization How to do Docker mirroring of Node service? Detailed explanation of extreme optimization Oct 19, 2022 pm 07:38 PM

During this period, I was developing a HTML dynamic service that is common to all categories of Tencent documents. In order to facilitate the generation and deployment of access to various categories, and to follow the trend of cloud migration, I considered using Docker to fix service content and manage product versions in a unified manner. . This article will share the optimization experience I accumulated in the process of serving Docker for your reference.

An in-depth analysis of Node's process management tool 'pm2” An in-depth analysis of Node's process management tool 'pm2” Apr 03, 2023 pm 06:02 PM

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

What to do if npm node gyp fails What to do if npm node gyp fails Dec 29, 2022 pm 02:42 PM

npm node gyp fails because "node-gyp.js" does not match the version of "Node.js". The solution is: 1. Clear the node cache through "npm cache clean -f"; 2. Through "npm install -g n" Install the n module; 3. Install the "node v12.21.0" version through the "n v12.21.0" command.

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to

See all articles