Deno introduction

Deno is a simple, modern, secure JavaScript and TypeScript runtime environment that uses V8 and is built into Rust to run. According to the official website, it has the following features:

  • The environment is safe.
  • supportTypeScriptOut of the box.
  • All you need is an executable file.
  • Built-in tools (eg: dependency checker, code formatter).
  • Contains a set of standard modules for auditing.

These are the highlights of the article. When we look at the feature list, we find the following advantages:

  • DenoBased on the latestJavaScriptLanguage;
  • DenoWith a broad coverage of the standard library;
  • Deno ไปฅ TypeScriptFor the core, with more unique ways to bring great advantages, including first-classTypeScriptSupport (DenoAutomatic compilationTypeScriptWithout you having to compile separately);
  • DenoStrong hugESModule standard;
  • DenoNo package manager;
  • DenoWith first-classawaitSyntax support;
  • DenoBuilt-in test tools;
  • DenoDesigned to be as compatible with browsers as possible, for example by providing built-in objectsfetchAnd globalwindowObject.

tips:

-Blair: Well, some of you might have a Question. Why is there denO after Node? And what is the difference between them? Answer: Because Node’s API is based on the callback mechanism, because Node was written before Promise and Async/Await definitions were defined in the standard. Completely new changes to this mechanism cannot be made in Node because such changes would have “devastating” effects. As a result, in Node we’re stuck calling back a lot of apis. But this is just an illustration of the problem and does not mean that Deno can replace Node.

Actual combat Demo

From the above brief introduction, we must have a general impression of it, let us use a small Demo to let everyone quickly get started.

Environment to prepare

Operating system: Mac OS Installation software: Homebrew(there are many installation methods, see the official website for details)

Command to introduce

  • bundle: Bundle the modules and dependencies of a project into a single file
  • cache: cache dependencies;
  • completions: Generate shell Completions;
  • doc: Displays the document of a module;
  • eval: Run a code such as deno eval “console.log(1 + 2);
  • fmt: built-in code formatter (similar to Gofmt in the Go language)
  • help: Prints a message or a help message to the stator command.
  • info: Displays information about cache or about source files
  • install: Installs the script as an executable;
  • repl: Start the REPL environment (default subcommand);
  • run: Runs a program with a given filename or URL;
  • test: Run tests;
  • types: Prints TypeScript declarations at runtime;
  • upgrade: Upgrade Deno to the latest version.

Writing and running

Code:

import { serve } from "https://deno.land/std/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}
Copy the code

Before running deno run app.ts, run deno run –allow-net app.ts.

If the following code is not executed, the following dependencies will be downloaded with an error:

Reason: Deno Security Sandbox

Sometimes you want to run a JavaScript program outside of your Web browser, but you don’t want it to have free access to whatever it wants on your system, such as the own secret key on your computer. Deno’s solution at this point is to try to borrow a lot from browsers to implement the same permissions model — any JavaScript running in the browser can’t do anything improper on your system unless you explicitly allow it.

Running results:

In addition to the above running network access, there are the following permission controls (which can be understood as RAM).

  • --allow-env: allows access to environment variables;
  • --allow-hrtime: Allows high resolution time measurement;
  • --allow-net=<allow-net>: Allows network access;
  • --allow-plugin: allows plug-ins to be loaded.
  • --allow-read=<allow-read>: allows the file system to read files.
  • --allow-run: allows child processes to run.
  • --allow-write=<allow-write>: allows file system write access.
  • --allow-all: Allows all permissions (the same as -a).

The above file is filled with a large block of code. We can run deno FMT app.ts to format the code in the file.

conclusion

The above is a brief introduction to a Deno project, of course, the appearance of Deno is not just a Hello World, in general, node does not meet and cannot be backward compatible. Of course, it does not mean that Deno is perfect, but what the development of backward compatibility will become, we have to follow The Times.

What Deno is currently doing:

  • DenoProvides a built-infetchImplementation that matches what is available in the browser.
  • DenoIn progress withNode.js stdlibCompatible layer.

For more information, please visit deno.land

The ninth essay calling activity is in progress, dear friends, please start now.portal