node deno_为什么要在Node.js上选择Deno的6个原因

node deno

Deno is a brand new way to write server-side JavaScript. It solves many of the same problems as Node.js. It was even created by the same programmer who created Node.js. Much like Node, it uses the v8 JavaScript engine under the hood. However, the rest of the runtime is implemented in Rust and JavaScript. Since it was announced, Deno has managed to achieve quite a bitof popularity. You can see it by checking their GitHub repository.

Deno是一种编写服务器端JavaScript的全新方式。 它解决了许多与Node.js相同的问题。 它甚至是由创建Node.js的同一位程序员创建的。 与Node非常相似,它在后台使用v8 JavaScript引擎。 但是,其余的运行时是在Rust和JavaScript中实现的。 自宣布以来,Deno设法获得了相当大的知名度。 您可以通过查看其GitHub存储库来查看它。

Image for post
GitHub repository GitHub存储库

Before digging in, let’s set up our machine with the mighty and secure Deno.

在深入研究之前,让我们使用强大而安全的Deno来设置机器。

安装 (Installation)

For Mac and Linux using shell, run:

对于使用shell的Mac和Linux,运行:

curl -fsSL https://deno.land/x/install/install.sh | sh

For Windows using power shell, run:

对于使用电源外壳的Windows,请运行:

iwr https://deno.land/x/install/install.ps1 -useb | iex

Also, you can install Deno using package managers such as Homebrew (Mac), Chocolatey (Windows), scoop(Windows), and cargo:

另外,您可以使用Homebrew(Mac), Chocolatey (Windows),Scoop(Windows)和cargo等软件包管理器安装Deno:

brew install denochoco install denoscoop install denocargo install deno

让我们弄清楚 (Let’s Figure It Out)

Our journey into Deno begins in a single TypeScript file. In this file, we have access to all the types in the runtime environment. This means we can write strongly typed code and get documentation directly in the IDE without ever needing to touch a TS config file. The features in the runtime can be accessed from the Deno namespace.

我们进入Deno的旅程始于单个TypeScript文件。 在此文件中,我们可以访问运行时环境中的所有类型。 这意味着我们可以直接在IDE中编写强类型代码并获得文档,而无需触摸TS配置文件。 可以从Deno名称空间访问运行时中的功能。

Let’s create a TypeScript file named main.ts in a working directory. Then, console.log the current working directory of the file system:

让我们在工作目录中创建一个名为main.ts的TypeScript文件。 然后, console.log文件系统的当前工作目录:

console.log(Deno.cwd());

We can execute our script with the following command:

我们可以使用以下命令执行脚本:

deno run main.ts
Image for post
Figure 2: Getting error without permissions.
图2:无权限获取错误。

1.内置权限 (1. Built-In Permissions)

The code above throws an error because Deno is secure by default. Deno is way better than Node at handling security. In order to run an application, for example, accessing your file system or the internet requires you to explicitly pass permissions.

上面的代码抛出错误,因为默认情况下Deno是安全的。 Deno在处理安全性方面比Node更好。 为了运行应用程序,例如,访问文件系统或Internet要求您显式传递权限。

If you import a package in Node and that package gets corrupted by someone taking over the package and injecting bad code, it would delete all of the files on your computer. But in Deno, unless you explicitly give your program the ability to delete files from your computer, the package that got corrupted is not going to be able to do anything because it doesn’t have the permission to.

如果您在Node中导入一个程序包,并且该程序包被某人接管并注入了错误的代码而损坏,它将删除计算机上的所有文件。 但是在Deno中,除非您明确赋予程序从计算机中删除文件的功能,否则损坏的程序包将无能为力,因为它没有权限。

Deno takes a great stance on security by having these permissions built in. You don’t have to worry about other packages doing things that you don’t want them to do on your computer or a web server.

Deno内置了这些权限,因此在安全性上具有非常好的立场。您不必担心其他软件包会执行您不希望它们在计算机或Web服务器上执行的操作。

So, we need to give permission to perform different actions in the runtime. Here, we can use the allow-read flag to allow this operation:

因此,我们需要授予在运行时执行不同操作的权限。 在这里,我们可以使用allow-read标志来允许此操作:

deno run --allow-read main.ts
Image for post
Figure 3: Correct output
图3:正确的输出

2.基于承诺 (2. Promise-Based)

It feels as though Deno’s developers have given much more attention to security. But my favorite aspect is how everything asynchronous is promise-based (farewell, callback).

似乎Deno的开发人员已经对安全性给予了更多关注。 但是我最喜欢的方面是异步的一切都是基于承诺的(告别,回调)。

As you can see, we can make a network request using the fetch API just like we do it in the browser. Because it supports top-level wait, we don’t even need an async function here. Not only that, but we can also start resolving promises without any extra boilerplate code:

如您所见,我们可以使用获取API发出网络请求,就像在浏览器中一样。 因为它支持顶级的wait ,所以我们这里甚至不需要异步函数。 不仅如此,我们还可以在没有任何额外样板代码的情况下开始兑现承诺:

const url = Deno.args[0];
const res = await fetch(url);

3. Deno尝试使您的代码尽可能与浏览器兼容 (3. Deno Attempts to Make Your Code as Browser-Compatible as Possible)

Deno contains a window object with lifecycle events that developers can listen to. Obviously, this makes the developer’s life easier:

Deno包含一个带有生命周期事件的窗口对象,开发人员可以收听。 显然,这使开发人员的生活更轻松:

window.onload = e => console.log(‘good bye nodejs’);

Oh wait! I forgot to mention that it can also execute web assembly binaries. According to its own website, “WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine.”

等一下! 我忘了提到它也可以执行Web程序二进制文件。 根据其自己的网站 ,“ WebAssembly(缩写为Wasm )是用于基于堆栈的虚拟机的二进制指令格式。”

const wbs = new Uint8Array([61,63,73]);
const wsm = new WebAssembly.Module(wbs);

4.包括一个标准库,其中包含一堆真正有用的软件包 (4. Includes a Standard Library With a Bunch of Really Useful Packages)

Deno takes it a step further and includes a standard library with a bunch of really useful packages for handling the date, time, colors, and things that aren’t already built into the browser. So, this allows you to take what the browser has and add to it.

Deno更进一步,它包括一个标准库,其中包含一堆非常有用的软件包,用于处理日期,时间,颜色和浏览器中尚未内置的内容。 因此,这使您可以利用浏览器的功能并将其添加到浏览器中。

The great thing about a standard library is that you can use it across all Deno projects instead of having to rely on importing modules from npm.

标准库的妙处在于,您可以在所有Deno项目中使用它,而不必依赖于从npm导入模块。

“Good code is short, simple and symmetrical — the challenge is figuring out how to get there.” — Sean Parent

“好的代码是简短,简单和对称的-挑战是弄清楚如何到达那里。” — 肖恩·父母

5.没有海量节点模块文件夹 (5. No Massive Node Modules Folder)

Instead, we import packages using the modern ES module syntax. Here, remote modules are referenced by their URL. When you run your script for the first time, it will download this code locally and cache it. There’s no package JSON and code can be referenced from any URL. It is very similar to how things work in the browser.

相反,我们使用现代ES模块语法导入包。 在这里,远程模块由其URL引用。 首次运行脚本时,它将在本地下载此代码并进行缓存。 没有包JSON,可以从任何URL引用代码。 这与浏览器中的工作方式非常相似。

As I mentioned before, there is no Node modules folder. All of that is just handled in the background for you. All of your dependencies are saved in a central location on your computer, so you don’t have to worry about having this massive modules folder or this really unwieldy package.json:

如前所述,没有Node modules文件夹。 所有这些只是在后台为您处理。 您所有的依赖项都保存在计算机的中央位置,因此您不必担心拥有如此庞大的modules文件夹或真正笨拙的package.json

import { Response } from “https://deno.land/std@0.63.0/http/server.ts";import { Server } from “https://deno.land/std@0.63.0/http/server.ts";
Image for post
Figure 4: Downloaded dependencies
图4:下载的依赖项

6.提供了一套解决常见用例的标准模块 (6. Provides a Set of Standard Modules to Solve Common Use Cases)

Also, Deno provides a set of standard modules to solve common use cases. For example, we can import serve from the HTTP module. Then we can use it to create a server that is treated as an async alterable. We can then await every request from the server and respond to it accordingly. That’s an awesome starting point for a server-side JavaScript app.

另外,Deno提供了一组标准模块来解决常见的用例。 例如,我们可以从HTTP模块导入serve 。 然后,我们可以使用它来创建被视为异步可变服务器的服务器。 然后,我们可以等待服务器的每个请求,并相应地对其进行响应。 这是服务器端JavaScript应用程序的绝佳起点。

Figure 5: Simple server
图5:简单服务器

This is just a simple introduction to Deno. If you want to find out more about Deno and its programming practices, visit the official website.

这只是对Deno的简单介绍。 如果您想了解有关Deno及其编程实践的更多信息,请访问官方网站

结论+缺点 (Conclusion + Drawbacks)

As you can see, Deno has a lot of additional features over Node. However, while it is really cool and has a lot of great features coming up, it is still only in its early stages. It just hit version 1 very recently, which means a lot of the things that Deno is trying to do are still on the way. For example, browser compatibility is still not 100%. They’re still implementing browser APIs and they’re going to be continuing to implement those as time goes on.

如您所见,Deno在Node上具有许多附加功能。 但是,虽然它确实很酷,并且具有许多强大的功能,但它仍处于早期阶段。 它最近才发布到版本1,这意味着Deno试图做的许多事情仍在进行中。 例如,浏览器兼容性仍然不是100%。 他们仍在实现浏览器API,随着时间的推移,他们将继续实现这些API。

Also, I mentioned that you don’t use npm with Deno. That is actually a little bit of a downside right now because JavaScript is based around npm. There are so many npm packages out there. The problem is that not all of these packages are going to be compatible with Deno. Also, it doesn’t have all of the Node-based APIs yet, so you are kind of missing out on a huge portion of what makes Node so popular.

另外,我提到您在Deno中不使用npm。 现在这实际上是一个缺点,因为JavaScript基于npm。 那里有很多npm软件包。 问题在于,并非所有这些软件包都将与Deno兼容。 而且,它还没有所有基于节点的API,因此您会错过使Node如此流行的很大一部分。

I think it is going to take a while before Deno starts to truly take off.

我认为Deno开始真正腾飞需要一段时间。

Thanks for reading! I hope you enjoyed the article.

谢谢阅读! 希望您喜欢这篇文章。

翻译自: https://medium.com/better-programming/6-reasons-why-you-should-choose-deno-over-node-js-3afd348feb3b

node deno

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值