Le guide
  • 📖Qui sommes-nous ?
  • prévention
    • 📱Les dangers des réseaux sociaux
    • 🔑Qu'est ce qu'un ransomware
    • 👀C'est quoi le phishing ?
    • 🌐Que peut-on faire avec une IP ?
    • Comment se rétracter lors d'un achat (ecommerce) ?
  • urgence
    • ⚡Cybermalveillance
    • 🩹Kit d'aide digital
    • 🚨Infos d'urgences
    • 📘Dernier poste
  • HELP
    • Dont ask to ask
    • Une alternative ?
    • 🎓Apprendre
      • 🇬🇧 Apprendre autre chose
      • Apprendre la cybersécurité
      • Apprendre le développement
      • liens utiles cybersec
      • Apprendre la création de jeux
      • Apprendre l’intelligence artificiel
      • Sauvegarder sous toutes les formes
  • Projets
    • Blukeys (en cours)
    • Communautés
      • Quark
  • Français
    • Comment bien écrire une liste ?
  • Cybersécurité - Hack
    • Outils
      • Introduction au reverse engineering
      • Outils de Base (introduction au Pentesting et la cybersécurité)
      • BurpSuite - Intercepter toutes les requêtes HTTP
      • 🇬🇧 fabacab/awesome-cybersecurity-blueteam
      • 🇬🇧 fabionoth/awesome-cyber-security
      • 🇬🇧 Bypassing Cloudflare WAF with the origin server IP address | Detectify Blog
    • OWASP Top 10
    • On dit chiffrer plutôt que crypter
    • Web security academy - WSA
    • Write-up ctf
      • TryHackMe - Retro
    • SANS - Formations en cybersécurité
  • Développement logiciels
    • Par où commencer ?
    • Principes et bonnes pratiques
      • POO (Programmation Orienté Objet)
    • Langages informatique
      • CSS, SASS
      • HTML
      • L'algorithmique
      • La famille C
      • La famille JS, TS
      • La famille JVM
      • PHP
      • Python
      • Ruby
      • RUST
      • SQL
    • Technologies
      • C'est quoi le Cloud Native ?
      • Installation Sqlite3 avec NPM ou Yarn
      • Les IDE recommandés
      • Base de données
      • Développement web sur windows
    • Toute les documentations
  • Cryptomonnaie
    • Tips navigateur Brave | Laisser un pourboire
  • Infrastructure
    • Générateur de configuration SSL
  • Qualité ingénierie logicielle
    • CI/CD Dév
    • CI/CD d'une infra
    • 🇬🇧 Introduction Mangle enables you to run chaos engineering experiments
      • Overview
      • Mangle Deployment and Administration Guide
      • Mangle Users Guide
      • Mangle Troubleshooting Guide
      • Mangle Developers' Guide
      • Contributing to Mangle
      • mangle-administration
        • Supported Deployment Models
          • Advanced Cassandra Configuration
        • Admin Settings
      • sre-developers-and-users
        • Adding Endpoints
        • Injecting Faults
          • Infrastructure Faults
          • Application Faults
          • Custom Faults
        • Requests and Reports
      • troubleshooting-guide
        • Deployment Stage
        • Boot/Initialization Stage
        • Endpoint Addition Stage
        • Fault Injection Stage
  • Web
    • Inspecteur de code web
    • Liste des APIs publique ( en cours )
    • Apprendre le SEO naturel
    • Tips pour fusionner des communautés Discord
    • Qu’est-ce que le rp ?
    • Google Admin Toolbox HAR Analyzer
  • Couche haute
    • HUGO
      • 🇬🇧 Quick Start
      • Mettre en place son premier site sous Hugo
    • 🇬🇧 Deno Introduction with Practical Examples
    • React-native
    • 🇬🇧 Angular HttpClient v9/8
    • 🇬🇧 Compiler un front Angular variabilisé comme un chef
    • 🇬🇧 Applying Angular Runtime Configurations in Dockerized Environments | Hacker Noon
  • Gaming
    • Installer Shadow Linux et Chrome book
    • Comment choisir son alimentation d'ordi ?
  • Linux
    • Git
    • Bash
    • Bases de linux
    • Installation LEMP sur Ubuntu 20.04 Digitalocean
    • 🇬🇧 Wireguard VPN on Ubuntu 20.04
    • Comment installer et utiliser Docker sur Ubuntu 20.04 | DigitalOcean
    • Sécuriser son serveur Linux sous Ubuntu
    • Introduction à tmux (terminal multiplexer)
  • Bot
    • Héberger son bot Discord
  • setup
    • Rainmeter personnalise ton Win10
  • Virtualisation
    • 🇬🇧 Dockerize Angular 9 App With Nginx
    • 🇬🇧 How To Remove Docker Images, Containers, Networks & Volumes
  • Architecture
    • L'Art De Créer Des Diagrammes d'Architecture
  • Marketing
    • Tips sur les réseaux sociaux 2021
  • Gestions IT (pro/perso)
    • Méthodes Agiles
    • Savoir utiliser la méthode GTD (Getting Things Done)
    • Méthode QQCOQP : analyse et résolution des problèmes
  • Nouveautés
    • 🇬🇧 Netflix-technologies
      • How Netflix Scales its API with GraphQL Federation (Part 1)
      • How Netflix Scales its API with GraphQL Federation (Part 2)
  • Autres
    • A classer
  • How to install Windows 10 root certificates [EASY STEPS]
  • 💕Nous soutenons
    • 💻Azales
    • Papi Uzumaki
    • 💻Beau de l'aire
    • Toshi
    • 🛡️ FCC (French Communuty Cybersecurity)
    • 🛡️Sharpforce
Propulsé par GitBook
Sur cette page
  • Key Takeaways
  • What is Deno?
  • Get Deno
  • Hands-on Deno
  • Example 2: Accessing the Command-line Arguments
  • Running Deno Programs
  • Deno Script Arguments
  • Creating a Web Server With Deno
  • First Web Server Example
  • Another Web Server Example: HTML
  • TypeScript in Deno?
  • Deno Package Management
  • Example of Importing a URL Module in Deno
  • Handling Files With Deno
  • Reading from a File in Deno
  • Writing to a File in Deno
  • Formatting Code
  • Comparisons With Node.js
  • Conclusion
  • About the Author

Cet article vous a-t-il été utile ?

Exporter en PDF
  1. Couche haute

🇬🇧 Deno Introduction with Practical Examples

PrécédentMettre en place son premier site sous HugoSuivantReact-native

Dernière mise à jour il y a 4 ans

Cet article vous a-t-il été utile ?

Key Takeaways

  • Deno is a JavaScript runtime built with security in mind.

  • Deno provides many helper functions to streamline frequent operations.

  • Deno supports non-transpiled TypeScript

  • Deno strives to address some challenges with Node.js

  • It’s incredibly easy to create web servers and manage files with Deno

What is Deno?

Deno is a simple, modern, and secure runtime for JavaScript and TypeScript applications built with the Chromium V8 JavaScript engine and Rust, created to .

Deno was originally announced in 2018 and reached 1.0 in 2020, created by the original Node.js founder Ryan Dahl and other .

The name DE-NO may seem odd until you realize that it is simply the interchange of NO-DE. The Deno runtime:

  • Adopts security by default. Unless explicitly allowed, Deno disallows file, network, or environment access.

  • Includes TypeScript support out-of-the-box.

  • Supports.

  • Includes built-in and code formatting (deno fmt).

  • Is compatible with browser JavaScript APIs: Programs authored in JavaScript without the Deno namespace and its internal features should work in all modern browsers.

  • Provides a one-file executable bundler through deno bundle command which lets you share your code for others to run without installing Deno.

Get Deno

The easiest way to install Deno is to use the deno_install scripts. You can do this on Linux or macOS with:

curl -fsSL https://deno.land/x/install/install.sh | sh

choco install deno

A successful install with Linux looks like this:

Hands-on Deno

Simple Example

Deno uses .js and .ts file extensions (though it will run any JavaScript or TypeScript file regardless of extension). Our first example demonstrates how we can safely write a browser-based Deno application, a simple JavaScript program that prints the current date and time.

// date_time.js

console.log(new Date());

Let’s run the code:

deno run date_time.js

Results:

2020-07-10T02:20:31.298Z

Example 2: Accessing the Command-line Arguments

With Deno, we can log command-line arguments with just one line of code:

// first_argument.js

console.log(Deno.args[0]);

Running Deno Programs

The run command runs any Deno code. And if you’re stuck, run deno --help, or deno -h, to get help using denocommands.

Deno also provides a way to run programs from a local file, a URL, or by integrating with other applications in the stdin by using "-" (without quotes).

Running code from filename:

deno run date_time.js

Running code from a URL is nearly identical:

deno run https://example.com/date_time.js

Read code from stdin:

echo "console.log('Hello Deno')" | deno run

Deno Script Arguments

Deno provides the args property from the Deno namespace (Deno.args) for accessing script arguments, this property returns an array containing the passed arguments at runtime.

A simple example below outputs the argument for a program with Deno.args.

// args.js

console.log(Deno.args, Deno.args.length);

Note: In Deno, anything passed after the script name will be passed as an argument and not consumed as a Deno runtime flag.

Creating a Web Server With Deno

First Web Server Example

Follow these steps to create a simple web server with Deno:

  1. Create a file, for example, server.js.

import { serve } from "https://deno.land/std/http/server.ts";
const server = serve({ port: 5000 });
console.log('Listening to port 5000 on http://localhost:5000');
for await (const server_request of server) {
  server_request.respond({ body: "Deno server created\n" });
}
  1. Run the code from the terminal. deno run --allow-net server.js

In this example, --allow-net provides network access permission to our program, otherwise Deno will throw a PermissionDenied error. This example:

  • Imports a module from the HTTP remote package.

  • Uses serve() method to create the server that listens on port 5000.

  • Loops through a promise to send the string "Deno server created\n" whenever any request is made to port 5000.

Another Web Server Example: HTML

To create an HTML web server in Deno, we will first have our HTML files that will be served whenever a user visits the server. To do this, follow the steps below:

  1. Create a folder for your project, for example, deno_server.

  2. Navigate to the folder from your terminal: cd deno_server

Create a file name index.html, and paste the following code into it:




    My First Deno Web page


    My First Deno Web page

  1. Create a file named server.js and paste the following code into it:

import { serve } from "https://deno.land/std/http/server.ts";
const server = serve({ port: 5000 });
for await (const server_request of server) {
  const _file = await Deno.readFile('./index.html');
  const decoder = new TextDecoder()
  server_request.respond({ body: decoder.decode(_file) });
}
  1. Go to your terminal and run the code: deno run --allow_net --allow_read server.js

Note: Because the code reads the index.html file and also performs a network operation, --allow-net and --allow-read are required for the code to run successfully.

This example:

  • Stores the hostname in a variable

  • Stores the port in a variable

  • Creates a loop that waits for a request from the client (e.g, user’s browser), reads a file from the server, and then sends the HTML file in response.

TypeScript in Deno?

Deno supports both JavaScript and TypeScript as its first-class languages, allowing you to leverage TypeScript source code directly in your Deno program without needing to first transpile to JavaScript.

A quick example of a TypeScript program in Deno:

// ask_details.ts
interface PersonDetails {
  name: string;
  age: number;
  phone: string;
}

function getDetails(details: any[]): PersonDetails {
  return {name: details[0], age: details[1], phone: details[2]};
}
console.log(getDetails(Deno.args));
// deno run ask_details.ts "John Bull" 53 "7676254544212"

Deno Package Management

Package management has been one of the factors for Node.js popularity, however this has also been a major bottleneck to its code instability and vulnerabilities. Node.js also predates JavaScript having a standard module format by several years. Instead of providing a standard package management system like npm, Deno instead recommends using standard ES module imports for loading URL modules. When URL modules are imported for the first time, Deno downloads and caches the module and its dependencies for later use. This also means Deno does not provide global CommonJS module functions like require() which exist in Node.js.

Example of Importing a URL Module in Deno

// imports the Deno standard datetime module
import { parseDate, parseDateTime } from 'https://deno.land/std/datetime/mod.ts'
parseDate("20-01-2019", "dd-mm-yyyy") // output : new Date(2019, 0, 20)
parseDate("2019-01-20", "yyyy-mm-dd") // output : new Date(2019, 0, 20)

This example:

  • Downloads the module and saves it in a cache directory, if not already cached, as specified by the DENO_DIR, or the default system's cache directory if DENO_DIR is not specified.

Handling Files With Deno

Reading from a File in Deno

The Deno namespace provides the open() method for reading files.

Let's create a file, sample.ts, and paste the following code:

// open() asynchronously returns the Uint8Array buffer of the file.
const file = await Deno.open('./sample.txt');
// TextDecoder decodes the Uint8Array to unicode text
const decoder = new TextDecoder('utf-8');
// text is decoded and saved.
const text = decoder.decode(await Deno.readAll(file));
console.log(text);

First let's run the code without the --allow-read flag to see how Deno behaves by default:

deno run sample.ts

This example throws a PermissionDenied error because we wanted to read a file without requesting permission, so let's re-run the example with the correct file access:

deno run --allow-read sample.ts

Writing to a File in Deno

The Deno.writeFile() method provides asynchronous methods for file writing. Let's explore with an example:

//create a text encoder that encodes utf8 string to Uint8Array
const encoder = new TextEncoder();
// encodes your utf8 text
const data = encoder.encode("Writing to file in Deno is simple!\n");
// asynchronously write to file 
// creates or overwrites a file
await Deno.writeFile("./sample1.txt", data);                 
// set permissions on new file
await Deno.writeFile("./sample3.txt", data, {mode: 0o777});  
// append the data to the file
await Deno.writeFile("./sample4.txt", data, {append: true}); 
// check if the file exists, if not, do nothing
await Deno.writeFile("./sample2.txt", data, {create: false});

Run the Deno file writing example with the --allow-write flag.

Let's read the sample1.txt file to confirm the write, the Deno namespace also provides readTextFile which can be easily used to read text files:

// Read the file to see if it truly writes.
console.log(await Deno.readTextFile('./sample1.txt'));

Formatting Code

To format all JS/TS files in the current directory including nested directories

deno fmt

To format specific files

deno fmt sample.txt sample1.txt

To format strings from the stdin

cat sample.ts | deno fmt -

To check the file format status

deno fmt --check

To check the format status of some files, space-separated

deno fmt --check sample.js sample.ts

To ignore formatting a section or block of code, precede the code section with // deno-fmt-ignore comment. For example:

// deno-fmt-ignore comment
const arrs = ["I", "will", "be", "left","unformatted"
               ];

To ignore formatting for an entire file, add // deno-fmt-ignore-file to the top of the file:

// deno-fmt-ignore-file
const arrs = ["I", "will", "be", "left","unformatted"  
 ];
const rand = Math.ceil(Math.random() * arrs.length) - 1;
// display a random element
console.log(arrs[rand]);

Comparisons With Node.js

Deno and Node.js have several key similarities and differences:

Environment

Deno

Node.js

JavaScript engine and technologies

Built with Chrome V8 and Rust

Built with Chrome V8 and C++

Package management

npm

Module loading

ES Modules

CommonJS or ES Modules

Default security restrictions

Restricts access to a file, network, and environment access.

Does not disallow access by default.

Conclusion

Congratulations on your practical introduction to using Deno. Here are few more resources to become familiar with Deno:

Please share any questions or feedback in the comments.

About the Author

Windows users can leverage :

Note: You may also need to .

There are .

Putting simplicity and security into consideration, Deno ships with some browser-related APIs which allows you to create a web server with little or no difference from a client-side JavaScript application, with APIs including fetch(), and WebAssembly.

You can create a web server in Deno by importing the http module from the . Although there are already many libraries out there, the Deno system has also provided a straightforward way to accomplish this.

Open your favorite browser and visit

Imports parseDate(), parseDateTime() methods from thefrom URL

The Deno FileSystem allows you to perform operating system tasks on files. Like other server programming languages and runtime, Deno also has quite a to read, write, append and remove files. Let's look at a few filesystem examples below.

Unlike Node.js which uses formatter libraries like , Deno includes an automatic formatter with the deno fmt command.

Intentionally does not encourage a single standard package manager (although )

Deno is still a very new environment with many promising features around security, speed, distribution, and language support. However, due to its infancy, Deno is not yet widely used or recommended for critical production applications. . Deno is open-source software available under the MIT license. Contributions are encouraged via theand should follow the.

— By Ryan Donovan

— By Michael Yuan

— By Diogo Souza

Erisan Olasheni is a Full-stack software engineer, freelancer, and a creative thinker who loves writing programming tutorials and tech tips. CTO at siit.co.

avoid several pain points and regrets with Node.js
mindful contributors
top-level await
unit testing
Chocolatey
export the deno directories to make the
deno
command globally available
additional ways to install Deno
Web Worker
official repo
http://localhost:5000/
Deno standard
DateTime
module
good number of file handling methods
Prettier
Deno Project
Deno contribution guidelines
Deno v1.0.0 released to solve Node.js design flaws
Deno Loves WebAssembly
Creating your first REST API with Deno and Postgres
the community is exploring some ideas