mirror of
https://github.com/rjNemo/deno_tuto
synced 2026-06-12 12:56:39 +00:00
examples
This commit is contained in:
commit
848b9f1f34
7 changed files with 108 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
.idea/
|
||||||
14
.vscode/settings.json
vendored
Normal file
14
.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"deno.enable": true,
|
||||||
|
"deno.unstable": true,
|
||||||
|
"deno.lint": true,
|
||||||
|
"deno.import_intellisense_origins": {
|
||||||
|
"https://deno.land": true,
|
||||||
|
},
|
||||||
|
"[typescript]": {
|
||||||
|
"editor.defaultFormatter": "denoland.vscode-deno",
|
||||||
|
},
|
||||||
|
"[typescriptreact]": {
|
||||||
|
"editor.defaultFormatter": "denoland.vscode-deno",
|
||||||
|
}
|
||||||
|
}
|
||||||
53
README.md
Normal file
53
README.md
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
# Deno
|
||||||
|
|
||||||
|
## 📑 Installation
|
||||||
|
|
||||||
|
On macOS use HomeBrew to install `deno` then update to latest version.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
brew install deno
|
||||||
|
deno upgrade
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚙️ Configuration
|
||||||
|
|
||||||
|
Create a basic configuration file using:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
code .vscode/settings.json
|
||||||
|
```
|
||||||
|
|
||||||
|
with the following content
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"deno.enable": true,
|
||||||
|
"deno.unstable": true,
|
||||||
|
"deno.lint": true,
|
||||||
|
"deno.import_intellisense_origins": {
|
||||||
|
"https://deno.land": true
|
||||||
|
},
|
||||||
|
"[typescript]": {
|
||||||
|
"editor.defaultFormatter": "denoland.vscode-deno"
|
||||||
|
},
|
||||||
|
"[typescriptreact]": {
|
||||||
|
"editor.defaultFormatter": "denoland.vscode-deno"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🧪 Test run
|
||||||
|
|
||||||
|
Create a `index.ts` file such as
|
||||||
|
|
||||||
|
```ts
|
||||||
|
console.log("Hello, Deno");
|
||||||
|
```
|
||||||
|
|
||||||
|
then run
|
||||||
|
|
||||||
|
```shell
|
||||||
|
deno run --allow-net index.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
Checkout files in [examples](examples/) folder.
|
||||||
14
examples/echo_server.ts
Normal file
14
examples/echo_server.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
// Run this example using
|
||||||
|
// deno run --allow-net examples/echo_server.ts
|
||||||
|
// nc localhost 8080
|
||||||
|
// echo
|
||||||
|
|
||||||
|
const hostname = "0.0.0.0";
|
||||||
|
const port = 8080;
|
||||||
|
|
||||||
|
const listener = Deno.listen({ hostname, port });
|
||||||
|
|
||||||
|
console.log(`Listen on ${hostname}:${port}`);
|
||||||
|
for await (const conn of listener) {
|
||||||
|
Deno.copy(conn, conn);
|
||||||
|
}
|
||||||
8
examples/fetch.ts
Normal file
8
examples/fetch.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
// Run this example using
|
||||||
|
// deno run --allow-net=example.com examples/fetch.ts
|
||||||
|
|
||||||
|
const url = Deno.args[0]; // catch the first argument passed to the script
|
||||||
|
const res = await fetch(url); //fetch over the net
|
||||||
|
|
||||||
|
const body = new Uint8Array(await res.arrayBuffer()); //convert buffer to bytes
|
||||||
|
await Deno.stdout.write(body); //write to terminal
|
||||||
10
examples/file.ts
Normal file
10
examples/file.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
// Run this example using
|
||||||
|
// deno run --allow-read examples/file.ts
|
||||||
|
|
||||||
|
const filenames = Deno.args;
|
||||||
|
|
||||||
|
for (const filename of filenames) {
|
||||||
|
const file = await Deno.open(filename);
|
||||||
|
await Deno.copy(file, Deno.stdout);
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
8
index.ts
Normal file
8
index.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { serve } from "https://deno.land/std@0.87.0/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" });
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue