Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
⌘K
Up or down tonavigateEnter toselectEscape toclose
On this page

Introduction to Deno APIs

Video descriptionJump to heading

In this video, we explore the powerful APIs provided by Deno in the globalnamespace. We demonstrate file system operations like creating, reading,writing, and appending to files using Deno's built-in methods. Then, examine howto handle command line arguments, environment variables, and set up a basicserver. We can reduce the need for external APIs with these Deno built-in APIs.

Transcript and examplesJump to heading

In the global name space, Deno has a ton of APIs that you can take advantage of.Let's take a look at a few of them.

Creating and writing to filesJump to heading

In order to write a file, first we will await Deno.open and we'll pass in thename of the file that we want to create. The second argument is going to be anobject where we'll setread,write andcreate totrue:

main.ts
await Deno.open("thoughts.txt",{  read:true,  write:true,  create:true,});

To run this, we will use:

deno main.ts

When run, the console will prompt us to allow read access, so we'll say yes (ory). Then it's going to ask us for write access, which is pretty cool (andwe'll allow that too withy), so we've granted both and now we have created afile calledthoughts.txt.

If we wanted to write some data to this file we could make some adjustments toourmain.ts file. Let's create a variable for our file (called file), thenwe're going to addappend:true to the object we pass to theDeno.open method(we can also get rid of create I suppose, since the file has already beencreated):

main.ts
const file=await Deno.open("thoughts.txt",{  read:true,  write:true,  append:true,});

Next, below this, we'll make a constant calledencoder, and make it equal anew text encoder. Then we'll make a second constant calleddata, which willcallencode. Finally we'll add a string with a newline and some text todata:

main.ts
const encoder=newTextEncoder();const data= encoder.encode("\nI think basil is underrated.");

Then we'llawait file.Write(data), which will take that data and write it tothe thoughts file, and finally we'll close the file.

await file.write(data);file.close();

This time we will run the file with the required permissions:

deno --allow-read --allow-write main.ts

If we take a look back at ourthoughts.txt file it will say "I think basil isunderrated". The text has been appended to our file.

Reading and appending to filesJump to heading

There are some other options as well, so let's go back to the top of our filethis time instead of usingDeno.open we'll useDeno.readFile. Which means wecan remove the second argument object, because we're being very specific aboutwhat we actually want to do here. Then we'll console log the file.

main.ts
const file=await Deno.readFile("thoughts.txt");console.log(file);

If we run this with:

deno --allow-read main.ts

The encoded file will be logged to the console, which isn't quite what I want. Iactually want the human readable text. So what I can do here is I can useDeno.readTextFile instead ofDeno.readFile, which will write the text fromthe file directly to the console.

We can also write to the file withDeno.writeTextFile. For example:

main.ts
await Deno.writeTextFile("thoughts.txt","Fall is a great season",);

Which, if we run withdeno --allow-write main.ts, will overwrite the contentsof thethoughts.txt file with the string about fall.

We can update that code to useappend: true:

main.ts
await Deno.writeTextFile("thoughts.txt","\nWinter is the most fun season!",{ append:true},);

If we run it again, withdeno --allow-write main.ts, it's going to append thesecond sentence to the end of the file.

Exploring command line argumentsJump to heading

We also have the option to explore command line arguments, so we could say:

main.ts
const name= Deno.args[0];console.log(name);

We can run this with our usual deno command, but this time pass in a commandlineargument, lets sayEve:

deno main.ts Eve

The nameEve will be logged to the console.

If we want to get fancy, we can update the logged template string to pass out amessage:

main.ts
const name= Deno.args[0];console.log(`How are you today,${name}?`);

Using env variablesJump to heading

On the Deno global, we also have environment variables. Let's create one calledhome, and log our home directory to the console:

main.ts
const home= Deno.env.get("HOME");console.log(`Home directory:${home}`);

When run withdeno main.ts, Deno will request environment access, which we canallow withy. Or we can run the command with the--allow-env flag, and ourhome directory will be logged to the console.

Setting up a simple HTTP serverJump to heading

Finally, lets look at our trustyserver constructor. We can create a handlerthat returns a response, and then pass that handler to theDeno.serve method.

main.ts
functionhandler(): Response{returnnewResponse("It's happening!");}Deno.serve(handler);

When run with

deno --allow-net main.ts

We'll see that a server is running and listening on port 8000. We can visitlocalhost:8000 in the browser and we should see the text "It's happening!".

So there are a ton of these that you can take advantage of but it's very nice toknow that we don't have to include an external library for everything, Deno hasus covered when it comes to managing errors handling servers and working withthe file system.

Find more videos in theExamples page and on ourYouTube channel.

Did you find what you needed?

What can we do to improve this page?

If provided, you'll be @mentioned in the created GitHub issue

Privacy policy

[8]ページ先頭

©2009-2025 Movatter.jp