In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are the differences between Deno and Node.js", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the difference between Deno and Node.js" article.
What is Deno and what are its main features?
Deno is a secure TypeScript runtime environment built on the Google V8 engine. Here are some of the materials used to build Deno:
Rust (the core module of Deno is written in Rust, and the core module of Node is implemented in C++)
Tokio (asynchronous programming framework implemented by Rust)
TypeScript (Deno supports both JavaScript and TypeScript out of the box)
V8 (Google produces JavaScript runtime, mainly used in Chrome and Node)
Let's take a look at what features Deno provides.
Security (rights management)
The most important feature of Deno is security.
Compared to Node,Deno 's default use of sandboxed environment to execute code, this means that the running environment does not have permission to operate the following modules:
File system
The network
Execute other scripts
System environment variable
Let's take a look at how Deno's permissions 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 and writes Hello world into them. But this code runs in a sandbox environment, so there is no permission to operate the file system.
It is also worth noting that in the above script we use the Deno namespace to manipulate files instead of using the fs module in Node. The Deno namespace provides a number of basic methods. However, using Deno namespaces can cause our code to lose browser compatibility, which we'll talk about later.
Execute the above script using the following command:
$deno run write-hello.ts
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)]
In fact, based on the script that created the file above, we will receive two permission prompts from the sandboxed environment. However, if we choose the allow always option, we will only be asked once.
If we choose deny, an error of PermissionDenied will be thrown, and if we do not write the error handling logic, the process will be terminated at this point.
If we execute the script with the following command:
Deno run-allow-write write-hello.ts
These two files are created without prompting.
Deno command line flag bits for file systems, in addition to-- allow-write, and-- allow-net/--allow-env/--allow-run, are used to turn on permissions for network, system environment variables, and operation child processes, respectively.
Module mechanism
Deno loads modules through URL in the same way as browsers. Many people are a little confused when they see URL in the server's import statement for the first time, but it's easy for me to understand:
Import {assertEquals} from "https://deno.land/std/testing/asserts.ts";
Do you think it's a big deal to introduce modules through URL? The answer is simple: by using URL to load modules, Deno can avoid introducing a centralized system like npm to release package,npm recently.
Introducing code through URL allows package authors to maintain and publish their own code in their favorite way. There will be no more package.json and node_modules.
When we start the application, Deno downloads all referenced files and caches them locally. Once references are cached, Deno will not download them unless we use the-relaod flag bit to trigger a re-download.
There are a few more issues worth discussing:
What if the site where the reference is stored is dead?
In the absence of a centralized package management site, sites that store modules may fail for a variety of reasons. If this happens in a development or even production environment, it is very dangerous!
As we mentioned in the previous section, Deno caches downloaded modules. Since the caches are stored on our local disk, the authors of Deno suggest that these caches be committed to the code repository. In this way, developers can still use the downloaded module even if the reference site is dead (except that the version is locked).
Deno stores the cache in the directory specified by the environment variable $DENO_DIR, and if we don't set this variable, it points to the system's default cache directory. We can specify $DENO_DIR to our local repository and submit them to the version management system (for example: git)
Can I only use URL to reference modules?
Always knocking on URL seems a bit XX, but fortunately, Deno offers two options to prevent us from becoming XX.
First, you can re-export the referenced module in the local file, such as:
Export {test, assertEquals} from "https://deno.land/std/testing/mod.ts";
Suppose the above file is called local-test-utils.ts. Now, if we want to use the test or assertEquals methods again, we just need to reference them as follows:
Import {test, assertEquals} from'. / local-test-utils.ts'
It can be seen that it is not the most important thing to quote them through URL.
The second scenario is to create a reference mapping table, such as a JSON file like this:
{"imports": {"http/": "https://deno.land/std/http/"}
Then import it into the code like this:
Import {serve} from "http/server.ts"
In order for it to work, we also need to introduce the import mapping table with the-importmap flag bit let Deno:
$deno run-- how does importmap=import_map.json hello_server.ts do version management
Version management must be supported by the package author so that it can be downloaded on the client side by setting the version number in URL: https://unpkg.com/liltest@0.0.5/dist/liltest.js.
Browser compatibility
Deno has plans to be compatible with browsers. Technically, with ES module, we don't need any packaging tools like webpack to run Deno code on browsers.
However, you can use tools like Babel to convert the code into an ES5 version of JavaScript, which is compatible with low-version browsers that do not support all the latest language features, resulting in a lot of unnecessary redundant code in the final file, increasing the size of the code.
The result depends on what our main purpose is.
TypeScript is supported out of the box.
You can easily use TypeScript in Deno without any configuration files. Of course, you can also write pure JavaScript code and use Deno to execute it.
The above is about the content of this article on "what is the difference between Deno and Node.js". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.