admin管理员组

文章数量:1593957

nodejs初学者书籍

If you observe carefully, DENO is an anagram of the word NODE. But probably much more than that as you start exploring it.

如果您仔细观察,DENO是NODE单词的一个类似物。 但是当您开始探索它时,可能还不止这些。

In 2018, Ryan Dahl, the original creator of Node.js, gave the famous talk “10 Things I Regret About Node.js” and announced his new project — Deno. He said, Deno aims to fix Node.js design mistakes and offers a new modern development environment. It has come a long way since then. Just yesterday, their twitter handle announced the release of version 1.4.2 with bug fixes.

2018年,Node.js的原始创建者Ryan Dahl发表了著名的演讲“我为Node.js感到遗憾的十件事”,并宣布了他的新项目Deno。 他说,Deno旨在解决Node.js设计错误并提供新的现代开发环境。 从那以后,它已经走了很长一段路。 就在昨天,他们的Twitter句柄宣布发布了具有错误修复的1.4.2版本。

Deno is a very modern, secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. The basic documentation for Deno is available on doc.deno.land.

Deno是一个非常现代的,安全JavaScript和TypeScript运行时,它使用V8并内置于Rust 。 Deno的基本文档位于doc.deno.land 。

Node is written in C++ and JavaScript but Deno is written in Rust and TypeScript. Deno’s best advantage is that it can afford to have everything re-written with modern technologies, since there’s no backward compatibility to maintain. Sounds very liberating!

Node用C ++和JavaScript编写,而Deno用Rust和TypeScript编写。 Deno的最大优势在于,它可以承受用现代技术重写的所有内容,因为无需维护向后兼容性。 听起来很解放!

默认TypeScript支持 (Default TypeScript Support)

If you love TypeScript, dislike millions of npm packages in your projects and can live without await, Deno is just what you’re looking for!

如果您喜欢TypeScript,则不喜欢项目中的数百万个npm软件包,并且可以不需await就可以生存,Deno正是您所需要的!

Deno has TypeScript at its core, which brings a huge advantage in many different ways including a first-class TypeScript support. Running TypeScript code with Deno does not require a separate compilation step as Deno does that automatically for you.

Deno以TypeScript为核心,以许多不同的方式带来了巨大的优势,包括一流的TypeScript支持。 与Deno一起运行TypeScript代码不需要像Deno那样自动为您执行的单独编译步骤。

Also the tools you use can infer many information about Deno as it is written in TypeScript. This means that while we code in VS Code, which obviously has a tight integration with TypeScript since both are developed at MicroSoft, we can get many benefits like type checking while coding and advanced IntelliSense features. The editor is very helpful many different way.

此外,您使用的工具还可以推断出许多用TypeScript编写的有关Deno的信息。 这意味着,虽然我们使用VS Code进行编码,而VS Code显然与TypeScript紧密集成,因为两者都是在MicroSoft上开发的,但我们可以获得许多好处,例如在编码时进行类型检查和高级IntelliSense功能。 该编辑器以许多不同的方式非常有用。

非常安全 (Very Secure)

Deno has a sandbox based security model at its core that prevents programs from doing anything other than what you allow. Deno also tries to replicate the same permission model that all the browser currently implement. No JavaScript running in the browser can do less secure things on your system unless you explicitly allow it.

Deno的核心是一个基于沙箱的安全模型,该模型可防止程序执行您允许的任何操作。 Deno还尝试复制所有浏览器当前实现的相同权限模型。 除非您明确允许,否则在浏览器中运行JavaScript不会在您的系统上执行较少的安全操作。

Also if a program want to access the network, then we need to give it explicit permission. You can achieve this by passing a flag when running the command.

另外,如果某个程序要访问网络,则需要给它明确的权限。 您可以通过在运行命令时传递一个标志来实现。

deno run --allow-net app.ts

Some more useful flags are below:

一些更有用的标志如下:

  • --allow-env allow environment access

    --allow-env允许环境访问

  • --allow-hrtime allow high resolution time measurement

    --allow-hrtime允许高分辨率时间测量

  • --allow-net=<allow-net> allow network access

    --allow-net=<allow-net>

  • --allow-plugin allow loading plugins

    --allow-plugin允许加载插件

  • --allow-read=<allow-read> allow file system read access

    --allow-read=<allow-read>允许文件系统读取访问

  • --allow-run allow running subprocesses

    --allow-run允许运行子进程

  • --allow-write=<allow-write> allow file system write access

    --allow-write=<allow-write>允许文件系统写访问

  • --allow-all allow all permissions (same as -A)

    --allow-all允许所有权限(与-A相同)

单个可执行文件 (A Single Executable File)

Deno comes with a single executable with no dependencies. You can install it using the installer commands or download a release binary from the releases page.

Deno带有一个没有依赖性的可执行文件。 您可以使用安装程序命令进行安装,也可以从发行版页面下载发行版二进制文件。

$ brew install deno

Unlike the modular binaries in Node.js, Deno is a single binary application. It also does not make use of the complex dependency management solutions like the NPM. Instead, developers declare dependencies in source code using direct URLs. There are already huge third-party Deno modules available. This approach is yet to be tested to scale large enterprise applications. As a result, Deno is in general not compatible with NPM packages written for Node.js.

与Node.js中的模块化二进制文件不同,Deno是单个二进制应用程序。 它还没有使用像NPM这样的复杂的依赖管理解决方案。 相反,开发人员使用直接URL在源代码中声明依赖项。 已经有大量的第三方Deno模块可用。 这种方法尚待测试以扩展大型企业应用程序。 因此,Deno通常与为Node.js编写的NPM软件包不兼容。

In addition to TypeScript and JavaScript, Deno applications can load and execute WebAssembly bytecode programs. A common example is the Sqlite module in Deno. It is compiled from C++ to WebAssembly. Also developers could extend Deno with Rust. This further allows the Rust dependency/package manage system — Cargo, to be used to manage Deno plugins and add-ons.

除了TypeScript和JavaScript,Deno应用程序还可以加载和执行WebAssembly字节码程序。 一个常见的示例是Deno中的Sqlite模块。 它是从C ++编译为WebAssembly。 开发人员也可以使用Rust扩展Deno。 这进一步允许Rust依赖项/程序包管理系统Cargo用于管理Deno插件和附加组件。

内置实用程序 (Built-in Utilities)

The Deno standard library is very young but its still very extensive. It includes many useful tools like:

Deno标准库虽然很年轻,但是仍然非常广泛。 它包括许多有用的工具,例如:

  • archive tar archive utilities

    archive tar存档实用程序

  • async async utilities

    async异步实用程序

  • bytes helpers to manipulate bytes slices

    bytes助手来操纵字节片

  • datetime date/time parsing

    datetime日期/时间解析

  • encoding encoding/decoding for various formats

    encoding各种格式的编码/解码

  • flags parse command-line flags

    flags解析命令行标志

  • fmt printing & formatting

    fmt打印和格式化

  • fs file system API

    fs文件系统API

  • hash crypto lib

    hash加密库

  • http HTTP server

    http HTTP服务器

  • io I/O lib

    io输入/输出库

  • log logging utilities

    log记录实用程序

  • mime support for multipart data

    mime支持多部分数据

  • node node.js compatibility layer

    node node.js兼容性层

  • path path manipulation

    path路径操作

  • ws websockets

    ws websockets

It also provides built-in tools like a test runner, a code formatter and a bundler.

它还提供了内置工具,例如测试运行器,代码格式化程序和捆绑程序。

格式化程序 (Formatter)

Deno has built-in utilities like a dependency inspector deno info and a code formatter deno fmt , just like Go, that automatically formats the script and adds semicolons if omitted anywhere.

与Go一样, deno fmt具有内置的实用程序,例如依赖项检查器deno info和代码格式化程序deno fmt ,它们会自动格式化脚本并添加分号(如果在任何地方都省略了的话)。

$ deno fmt index.ts

审核标准模块 (Audited Standard Modules)

Deno provides a curated list of audited standard modules that are reviewed by the Deno maintainers and are guaranteed to work with a specific Deno version. These reside alongside the Deno source code in the denoland/deno repository.

Deno提供了经过审核的标准模块的精选列表,这些模块已由Deno维护人员进行审查,并保证可以与特定的Deno版本一起使用。 它们与Deno源代码一起存储在denoland / deno存储库中。

These standard reviewed modules are hosted at deno.land/std and are distributed via URLs like all other ES modules that are compatible with Deno.

这些经过标准审查的模块托管在deno.land/std中,并且通过与Deno兼容的所有其他ES模块一样的URL进行分发。

These modules listed above do not have external dependencies and they are reviewed by the Deno core team. The idea is to have a standard set of high quality code that all Deno projects can use fearlessly.

上面列出的这些模块没有外部依赖性,并且由Deno核心团队进行审查。 这个想法是要有一套标准的高质量代码,所有Deno项目都可以毫不费力地使用它。

These modules will eventually be tagged in accordance with Deno releases but as of today we do not yet consider them stable and so we version the standard modules differently from the Deno runtime to reflect this.

这些模块最终将根据Deno发行版进行标记,但是截至目前,我们还不认为它们是稳定的,因此我们对标准模块的版本与Deno运行时有所不同,以反映这一点。

没有包管理器 (No Package Manager)

Deno does not use the CommonJS format and doesn’t provide a package manager like npm. All modules are loaded directly in code using just an URL.

Deno不使用CommonJS格式,也不提供像npm这样的包管理器。 仅使用URL将所有模块直接加载到代码中。

你好世界的例子 (Hello World Example)

import { serve } from “https://deno.land/std@0.70.0/http/server.ts";const s = serve({ port: 8000 });for await (const req of s) {  req.respond({ body: “Hello World\n” });}

If you want to run the program again, it’s now cached by Deno and it does not need to download it again. You may force a reload of the original source with the --reload flag.

如果您想再次运行该程序,则它现在已由Deno缓存,不需要再次下载。 您可以使用--reload标志强制重新加载原始源。

There are some advantages of using the Deno style. By importing code via URL, it’s possible to host modules everywhere and anywhere on the internet. Deno packages can also be distributed without a centralized registry and avoid any corporate control. There is also no need for the package.json file and a dependency list, because all modules are downloaded, compiled and cached on the application run. This helps to remove the dependency on a centralized registry like npm for Node.js which is now controlled by Microsoft.

使用Deno样式有一些优点。 通过URL导入代码,可以在Internet上的任何地方托管模块。 Deno软件包也可以在没有集中式注册表的情况下分发,并避免任何公司控制。 也不需要package.json文件和依赖项列表,因为所有模块都是在应用程序运行时下载,编译和缓存的。 这有助于消除对像Node.js的npm这样的集中式注册表的依赖,该注册表现在由Microsoft控制。

第三方模块 (Third Party Modules)

Deno can import modules from any location on the internet, like GitHub, a personal web server, or a CDN like AWS, Skypack or jspm.io. This can also do away with the monopoly of a controlled registry.

Deno可以从Internet上的任何位置导入模块,例如GitHub,个人Web服务器或CDN(例如AWS,Skypack或jspm.io)。 这也可以消除对受控注册表的垄断。

As Microsoft is tightening its grip around open source projects, it could allow Microsoft to dictate the policies around these open source projects in future. When Microsoft acquired GitHub, many open source developers moved to alternate platforms like GitLab though GitHub remains the first choice for the developers.

随着Microsoft加强对开源项目的控制,它可能允许Microsoft将来针对这些开源项目制定政策。 当微软收购GitHub时,尽管GitHub仍然是开发人员的首选,但许多开源开发人员都迁移到了GitLab等替代平台。

To make it easier to consume third party modules, Deno also provides some built in tooling like deno info and deno doc. Also deno.land provides a simple public hosting service for ES modules that work with Deno. It can be found at deno.land/x. Basically deno.land/x is a hosting service for Deno scripts. It caches releases of most open source modules stored on GitHub and serves them from one domain.

为了使使用第三方模块更容易,Deno还提供了一些内置工具,例如deno infodeno doc 。 deno.land还为与Deno合作的ES模块提供了简单的公共托管服务。 可以在deno.land/x找到。 基本上deno.land/x是Deno脚本的托管服务。 它缓存存储在GitHub上的大多数开源模块的发行版,并从一个域提供服务。

For example — lodash, a modern JavaScript utility library delivering modularity, performance, & extras can be found here : lodash@4.17.19

例如可以在此处找到lodash,这是一个提供模块化,性能和其他功能的现代JavaScript实用程序库: lodash@4.17.19

拥抱ES模块 (Embraces ES Modules)

As you all know, Node uses the CommonJS syntax for importing packages. Deno uses ES Modules but the official way. It uses modern ECMAScript features in all its API and standard library, while Node.js only uses a callbacks-based standard library and has no plans to upgrade it.

众所周知,Node使用CommonJS语法导入软件包。 Deno使用ES模块,但是是官方方式。 它在所有API和标准库中都使用了现代ECMAScript功能,而Node.js仅使用基于回调的标准库,没有计划对其进行升级。

头等舱 (First Class Await)

With Debo, we can use the await keyword without wrapping it into an async function because Deno implements top-level await. When using async/await in Node.js, you will have to wrap the await inside of an asynchronous function, and you have to label it async. Deno makes it possible to use the await function in the global scope without having to wrap it inside an async function which is an awesome feature.

使用Debo,我们可以使用await关键字而无需将其包装到async函数中,因为Deno实现了顶级await。 在Node.js中使用async / await时,必须将await包装在异步函数中,并且必须将其标记为async 。 通过Deno,可以在全局范围内使用await函数,而不必将其包装在一个很棒的异步函数中。

浏览器兼容 (Browser Compatible)

Deno has always aimed to be browser-compatible as far as possible. It provides a built-in fetch and the global window object. The built-in fetch implementation matches the one available in the browser currently.

Deno一直致力于尽可能与浏览器兼容。 它提供了内置的fetch和全局window对象。 内置的fetch实现与当前浏览器中可用的实现匹配。

Deno highly supports V8 Inspector Protocol which makes it possible to debug Deno programs using Chrome Devtools or other clients that support the protocol like VS Code. To activate debugging capabilities, please run Deno with flags like--inspect or --inspect-brk— inspect flag allows attaching a debugger at any point in time, and — inspect-brk will wait for debugger breakpoint and pause execution on the first line of code.

Deno高度支持V8 Inspector协议,这使得可以使用Chrome Devtools或其他支持该协议(如VS Code)的客户端来调试Deno程序。 要激活调试功能,请使用--inspect--inspect-brk等标志运行--inspect-brk inspect标志允许在任何时间点附加调试器,并且--inspect-brk -brk将等待调试器断点并在第一行暂停执行代码。

内置测试支持 (Built-in Testing Support)

Deno has a built-in test runner that allows us to test our JavaScript and TypeScript code. The syntax is very similar to the one used by JavaScript testing libraries like Jest or Jasmine.You can simply use the test functionality on the Deno namespace to create a test. We can then run our tests with the deno test command. With the testing module, you can run a full test suite by pointing your deno test command to a test suite hosted somewhere online without having to pull down and run the test suite yourself.

Deno具有内置的测试运行程序,可让我们测试JavaScript和TypeScript代码。 该语法与Jest或Jasmine之类JavaScript测试库所使用的语法非常相似。您可以简单地在Deno名称空间上使用测试功能来创建测试。 然后,我们可以使用deno test命令运行测试。 使用测试模块,您可以通过将deno test命令指向在线某个地方托管的测试套件来运行完整的测试套件,而无需亲自下拉并运行该测试套件。

热门图书馆供应情况 (Popular Library Availability)

Deno also provides support for very popular libraries and middle-wares which NodeJs has used thus far. Some are listed below:

Deno还为NodeJs迄今为止使用的非常流行的库和中间件提供支持。 下面列出了一些:

  • deno-drash — A REST micro framework for Deno’s HTTP server with zero dependencies

    deno-drash —用于Deno的HTTP服务器的REST微框架,具有零依赖性

  • deno-express — A solution to run a Deno Webserver like the node express way

    deno-express —一种像节点表达方式一样运行Deno Web服务器的解决方案

  • oak — Oak is interesting because it’s inspired by Koa

    橡木-橡木很有趣,因为它的灵感来自Koa

  • pogo — Pogo is an easy-to-use, safe, and expressive framework for writing web servers and applications and is inspired by hapi

    pogo -Pogo是一个易于使用,安全且富有表现力的框架,用于编写Web服务器和应用程序,并受到hapi的启发

  • servest — A progressive http server for Deno

    servet — Deno的渐进式http服务器

Also Deno has a compatibility layer with the Node.js stdlib in progress.

Deno还在与Node.js stdlib兼容。

云端支援 (Cloud Support)

Deno has just reached v1.0 but is already supported by notable players in cloud computing industry, like major cloud providers such as AWS Lambda, Azure Functions and Google Cloud Run.

Deno刚刚达到v1.0,但已经得到了云计算行业中知名企业的支持,例如AWS Lambda,Azure Functions和Google Cloud Run等主要云提供商。

结论 (Conclusion)

Finally the big question — Will it replace Node.js?

最后一个大问题-它会取代Node.js吗?

May be not today as Node.js is a well established giant with incredibly well supported technology. The prominent goal of Deno is not to replace Node.js, but to offer an alternative for the next tech generation. Right now, some of the differences are quite controversial and it’s very hard to predict if they will work out in a feasible way. But all Node.js programmers should closely monitor this project.

可能不是今天,因为Node.js是一个成熟的巨人,拥有令人难以置信的技术支持。 Deno的主要目标不是取代Node.js,而是为下一代技术提供替代方案。 目前,其中一些差异颇具争议,很难预测它们是否会以可行的方式解决。 但是所有Node.js程序员都应密切监视此项目。

While most are optimistic about Deno’s future, but majority of developers believe that Node.js is here to stay. It will be very formidable for Deno to overtake Node.js in the present day due to the large ecosystem and enterprise applications around Node.js.

尽管大多数人对Deno的未来感到乐观,但大多数开发人员都认为Node.js会留下来。 由于围绕Node.js的大型生态系统和企业应用程序,对于Deno来说,今天超越Node.js将是非常艰巨的。

但是总会有一个杀死巨人的大卫。 (But there is always a David that kills the Goliath.)

翻译自: https://medium/@gp.greatworld/deno-the-successor-to-nodejs-b884f0079a91

nodejs初学者书籍

本文标签: 初学者书籍nodejs