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.
[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); })();
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.
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.
$ deno run write-hello.tsAfter 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)]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.
deny, an error of
PermissionDenied will be thrown. If we do not write error handling logic, the process will be terminated at this time.
deno run --allow-write write-hello.tswill 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";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
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
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";
假如上面这个文件叫local-test-utils.ts
。现在,如果我们想再次使用test
或者assertEquals
方法,只需要像下面这样引用它们:
import { test, assertEquals } from './local-test-utils.ts';
看得出来,是不是通过URL来引用它们并不是最重要的啦。
第二种方案,建一个引用映射表,比如像下面这样一个JSON文件:
{ "imports": { "http/": "https://deno.land/std/http/" } }
然后把它像这样import到代码里:
import { serve } from "http/server.ts";
为了让它生效,我们还需要通过--importmap
标志位让Deno来引入import映射表:
$ deno run --importmap=import_map.json hello_server.ts
如何进行版本管理
版本管理必须由包作者来支持,这样在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!

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)