preface

The grass is green again in spring. Countless technical frameworks are being updated and growing. So what’s the big deal? I thought, Deno, worth it!

Deno introduction

Deno is a simple, modern, secure JavaScript and TypeScript runtime environment that uses the V8 engine and is built on Rust.

V8 is an open source Java scripting engine developed by the Chromium project for GoogleChrome and chrome web browsers.

Rust is a multi-paradigm programming language that focuses on performance and security, especially secure concurrency.

Deno, an operating framework released in 2020, has the following features:

  • This is safe by default. Files, networks, or environments cannot be accessed unless explicitly enabled.

  • Support for TypeScript out of the box.

  • Distribute only a single executable (denO).

  • There are built-in utilities such as dependency information viewer (deno Info) and code formatter (deno FMT).

  • There is a standard set of vetted modules

Deno history

Deno architecture

Deno installation

Installation is fairly simple. Deno runs on macOS, Linux, and Windows. Deno is a single executable with no additional dependencies.

There are two ways to install Deno.

  1. Official download, manual installation
  2. Use the command to automatically install
The official download

The release file can be downloaded from the official deno release page.

In Windows, download the deno-x86_64-PC-Windows-msvc. zip file and decompress it to form an independent executable exe file.

Command to install

Use different installation tools for different systems

Shell (Mac, Linux):

$curl -fsSL https://deno.land/x/install/install.sh | sh
Copy the code

PowerShell (Windows):

$iwr https://deno.land/x/install/install.ps1 -useb | iex
Copy the code

Homebrew (Mac):

$brew install deno
Copy the code

Chocolatey (Windows):

$choco install deno
Copy the code

Scoop (Windows):

$scoop install deno
Copy the code

Build and install from source using Cargo

$cargo install deno
Copy the code

Refer to deno_install for more installation instructions

Hello World

After the download and installation, we can practice our HelloWorld program. Use the official welcome.ts for the demonstration.

Download the Welcome. Ts

Run the command:

deno run https://deno.land/std/examples/welcome.ts
Copy the code

More sophisticated attempts

Write a TS file

import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello,First Deno Program\n" });
}
Copy the code

run

deno run --allow-net ts/test.ts
Copy the code

Abnormal running

In the more complex attempt above, we can see the –allow-net parameter added to the command.

Normal running command

deno run test.ts
Copy the code

Why does the exception occur?

By default, Deno is secure. Therefore, the denO module has no redundant functionality for files, networks, or environments unless we specifically enable it. So, we need to explicitly grant permissions.

– allow-net – Access the network

– allow-read – Reads files

– allow-write – Write files

Deno standard library

Deno provides a set of standard modules that are audited by the core team and guaranteed to work on Deno. Library address: deno.land/ STD /

Deno third-party library

Deno can import modules from anywhere on the Web.

To make it easier to use third-party modules, Deno comes with built-in tools such as Deno Info and Deno Doc.

conclusion

Let’s summarize everything we learned about getting started with deno JS.

  • Deno is not a replacement for Node JS.

  • Deno is a safe runtime environment for JavaScript and TypeScript.

  • If typescript is widely used, deno.js allows additional javascript compilers, such as Babel, to be removed.

  • Deno does not use NPM, nor does it use NPM package.json.

If you are still in doubt, please refer to the denO manual for more information.

In 2021, new try, new start. Do it!!

Activity link: juejin.cn/post/693197…