Learn Deno and its Basics Part-1
What is Deno?
Deno is a modern and secure runtime language for Javascript and Typescript. It is very similar to Node.js
The advantages of using Deno are:
- Default security : By default Deno does not have access to the network, filesystem, and environment variables. We have to explicitly grant these permissions.
- Web compatible : Deno has a set of Apis that are compatible with API web standards like fetch, FormData, URL, URLSearchParams, Worker, atob, btoa, and more.
- Standard Library : Deno has a list of standard modules that are guaranteed to work with a specific Deno version such as UUID, hash, HTTP, and more. These modules are maintained by Deno maintainers.
- Deno supports TypeScript out of the box.
- Deno has built-in utilities such as formatted, linter, test runner, and document generator.
After knowing about Deno. Since Deno is considered a successor to the famous Node.js. Let's understand the difference.
What is the difference between Deno and Node.js?
these are the some common differences between these languages.you can see it on given table
Basis | Deno | Node |
---|---|---|
Engine | v8 | v8 |
Host Language | Rust | c++ |
Module syntax | ESM | Common JS |
APIs | Deno global + Web Api + standard library | Built-in modules |
TypeScript | Out of the box | Config |
Top level async wait | Yes | No |
Package Manager | No import via URL | npm |
Let's get our hands dirty now.
How to install Deno?
- Go to Deno.land
- Based on your operating system select the option.
- I am using windows and I am using chocolatey. So I will use the command
Choco install deno
- After that, open PowerShell and run the command.
- Run deno –version to confirm you have successfully installed Deno. Please use PowerShell with administrative rights.
No Tools Or Config Required
Deno can run JavaScript or TypeScript out of the box with no additional tools or config required.
Examples.
JavaScript :
1
2
3
4
5
6
// app.js
function sum(a, b) {
return a + b
}
console.log(sum(3, 5)) // 8
TypeScript:
1
2
3
4
5
6
// app.ts
function sum(a: number, b: number) {
return a + b
}
console.log(sum(3, 5)) // 8
Deno Command Line Interface:
- Command: deno help
This will provide you with important commands that can be used.
For eg: deno info, deno install, deno repl
Mostly we use deno run command that we use to run a file. We can also get all types of run commands by entering deno help run
Mostly we use deno run
command that we use to run a file. We can also get all types of run commands by entering deno help run
This will give you more information about deno run commands. As you can see most of the run commands require permission.
Let's try some of the run commands that do not require permission. For this, we will write typescript code in the basic folder. I will create a file app.ts.
Run the basic hello world program.
Web API :
Deno comes with web api by default. In node, we don’t have a web API. Let’s see them in practice.
Prerequisites:
- Install deno for vs code extension
- In setting—>jsonsetting enter deno.enable =”true”
- Fetch web Api in Deno
We have to give permission to allow network access deno run --allow-net app.ts
Here we are using web API to fetch data and then consoling in the program.
- Blob
This is used to fetch large binary objects including media and files. We are just using array values to show you this example.
Similarly, we can use form data
More details of web API can be found here: https://deno.land/api@v1.28.1
Deno Specific APIs
Besides web APIs deno has specific APIs which generally starts with deno. Some of these apis are read and write files. Let’s see them in action.
Read Apis
There are several ways/APIs to read in deno. We are going to learn and talk about three of them.
1. Fn Deno.readTextFile
This is an asynchronous function so we have to use await in this. We have to give permission to read deno –allow-read app.ts
Read text file return a promise with a string value.
2. Fn Deno.readFile
This is an asynchronous function so we have to use await in this. We have to give permission to read deno run –allow-read app.ts. But this will return the promise of value uint
You can decode the text of the string using
1
2
3
4
const data = await Deno.readFile('./text.txt')
const decoder = new TextDecoder('utf-8')
console.log(data)
console.log(decoder.decode(data))
PS C:\Users\4 way technologies\Deno\basic\basic> deno run --allow-read app.ts
Uint8Array(27) [
87, 101, 108, 99, 111, 109, 101,
32, 116, 111, 32, 68, 101, 110,
111, 32, 68, 101, 118, 101, 108,
111, 112, 109, 101, 110, 116
]
Welcome to Deno Development
- Fn Deno.readAll
To read the file using this function we need to open the file first this also returns the promise value in uintarray8
1
2
3
4
const file = await Deno.open('./text.txt')
const data = await Deno.readAll(file)
Deno.close(file.rid)
console.log(data)
With output
PS C:\Users\4 way technologies\Deno\basic\basic> deno run --allow-read app.ts
Uint8Array(27) [
87, 101, 108, 99, 111, 109, 101,
32, 116, 111, 32, 68, 101, 110,
111, 32, 68, 101, 118, 101, 108,
111, 112, 109, 101, 110, 116
]
A science graduate who has a keen interest to lean about new technologies and research area. With an experience in the field of data analytics and content writing, she aims to share her knowledge among passionate tech readers.