笔记03 Deno

Deno 官网

Deno

1 控制台打印

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
console.log("Welcome to Deno 🦕");
C:\Users\Administrator
λ deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Warning Implicitly using latest version (0.73.0) for https://deno.land/std/examples/welcome.ts
Download https://deno.land/std@0.73.0/examples/welcome.ts
Check https://deno.land/std@0.73.0/examples/welcome.ts
Welcome to Deno �

2 发出一个 HTTP 请求

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const url_ = Deno.args[0];
const res = await fetch(url_);

// TODO(ry) Re-enable streaming in this example.
// Originally we did: await Deno.copy(res.body, Deno.stdout);
// But maybe more JS-y would be: res.pipeTo(Deno.stdout);

const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);
C:\Users\Administrator
λ deno run https://deno.land/std/examples/curl.ts https://example.com
Download https://deno.land/std/examples/curl.ts
Warning Implicitly using latest version (0.73.0) for https://deno.land/std/examples/curl.ts
Download https://deno.land/std@0.73.0/examples/curl.ts
Check https://deno.land/std@0.73.0/examples/curl.ts
error: Uncaught PermissionDenied: network access to "https://example.com/", run again with the --allow-net flag
    at Object.jsonOpAsync (core.js:236:13)
    at async fetch (deno:op_crates/fetch/26_fetch.js:1272:29)
    at async https://deno.land/std@0.73.0/examples/curl.ts:3:13
C:\Users\Administrator                                                                                                                     
λ deno run --allow-net=example.com https://deno.land/std/examples/curl.ts https://example.com                                              
<!doctype html>                                                                                                                            
<html>                                                                                                                                     
<head>                                                                                                                                     
    <title>Example Domain</title>                                                                                                          

    <meta charset="utf-8" />                                                                                                               
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />                                                                  
    <meta name="viewport" content="width=device-width, initial-scale=1" />                                                                 
    <style type="text/css">                                                                                                                
    body {                                                                                                                                 
        background-color: #f0f0f2;                                                                                                         
        margin: 0;                                                                                                                         
        padding: 0;                                                                                                                        
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

    }                                                                                                                                      
    div {                                                                                                                                  
        width: 600px;                                                                                                                      
        margin: 5em auto;                                                                                                                  
        padding: 2em;                                                                                                                      
        background-color: #fdfdff;                                                                                                         
        border-radius: 0.5em;                                                                                                              
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);                                                                                      
    }                                                                                                                                      
    a:link, a:visited {                                                                                                                    
        color: #38488f;                                                                                                                    
        text-decoration: none;                                                                                                             
    }                                                                                                                                      
    @media (max-width: 700px) {                                                                                                            
        div {                                                                                                                              
            margin: 0 auto;                                                                                                                
            width: auto;                                                                                                                   
        }                                                                                                                                  
    }                                                                                                                                      
    </style>                                                                                                                               
</head>                                                                                                                                    

<body>                                                                                                                                     
<div>                                                                                                                                      
    <h1>Example Domain</h1>                                                                                                                
    <p>This domain is for use in illustrative examples in documents. You may use this                                                      
    domain in literature without prior coordination or asking for permission.</p>                                                          
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>                                                          
</div>                                                                                                                                     
</body>                                                                                                                                    

3 读取一个文件

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
const filenames = Deno.args;
for (const filename of filenames) {
  const file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}
C:\Users\Administrator
λ deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd
Download https://deno.land/std/examples/cat.ts
Warning Implicitly using latest version (0.73.0) for https://deno.land/std/examples/cat.ts
Download https://deno.land/std@0.73.0/examples/cat.ts
Check https://deno.land/std@0.73.0/examples/cat.ts
error: Uncaught NotFound: 系统找不到指定的路径。 (os error 3)
    at Object.jsonOpAsync (core.js:236:13)
    at async Object.open (deno:cli/rt/30_files.js:44:17)
    at async https://deno.land/std@0.73.0/examples/cat.ts:4:16