Deno 1.0 has just been released, so today we are going to use Deno to develop a simple Web service.

About Deno

Deno pronunciation

There are currently two pronunciations of deno, tino, and I personally think tino is more appropriate

Deno life

Deno was founded in 2017 by Ryan Dahl, who is also the founder of Node. If you are interested in why you are unhappy with Node and why you started Deno, please Google.

Deno environment

Deno is a server runtime developed in Rust that supports multiple languages and can run JavaScript, TypeScript, and WebAssembly applications directly.

Deno vs Node

  • You can run it directlyTypeScript
  • Brand new package management, all modules imported by URL, nonpmNothing heavynode_modules
  • Security control, script by default does not have read and write permissions, network permissions, must use parameters to explicitly open permissions, mom no longer need to worry about other people’s code running around in my program
    • --allow-readWith read permission enabled, you can specify readable directories, for example--allow-read=/temp.
    • --allow-writeEnable the write permission.
    • --allow-netAllows network communication by specifying requestable domains
    • --allow-envAllows reading of environment variables
    • Permission declarations should come first:deno run --allow-net index.ts

Install Deno

  • MAC and Linux

    #Methods a
    #Configure environment variables yourself
    $ curl -fsSL https://deno.land/x/install/install.sh | sh
    
    #Method 2
    #One-step, no need to configure environment variables (recommended)
    $ curl -fsSL https://deno.land/x/install/install.sh | sudo DENO_INSTALL=/usr/local sh
    Copy the code
  • windows

    $env:DENO_INSTALL = "C:\Program Files\deno" iwr https://deno.land/x/install/install.ps1 -useb | iex
    Copy the code

VSCode plug-in

Search deno in VSCode, there will be two plug-ins, one author axetroy and one justjavac. Here, we first use justjavac to develop the plug-in. Although axetroy is recommended by the official website, it has not been maintained for a long time, so it can no longer be used normally. The official plug-in will be launched later

Hello world

So deno is ready to go, so I’m going to say hello world as usual

  • Create the deno-Demo folder

    $ mkdir deno-demo
    $ cd deno-demo
    Copy the code
  • A new index. Ts

    // deno-demo/index.ts
    console.log('hello world');
    Copy the code
  • run

    $ deno index.ts
    Copy the code
  • Hello world output

The Web service

  • code
    // deno-demo/index.ts
    import { Application } from 'https://deno.land/x/abc/mod.ts';
    const app = new Application();
    app.get('/hello', (c) => {
      return {
        msg: 'hello world for get'}; }); app.post('/hello', (c) => {
      return {
        msg: 'hello world for post'}; }); app.start({port: 8080 });
    Copy the code
  • run
#You need Internet access, plus --allow-net,
$ deno run --allow-net index.ts
Copy the code

Allow-net allow-net allow-net allow-net allow-net

Afterword.

For Deno, I am very optimistic, TS, JS people would have no language learning barrier, and using Deno to write TS is lighter and more elegant, better development experience, more secure runtime, the potential is infinite.