Categories
Deno

Deno — HTTP Requests and File Manipulation

Spread the love

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.

Managing Dependencies

We can manage dependencies in our Deno apps.

We can reexport modules with the export statement:

math.ts

export {
  add,
  multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";

index.ts

import { add, multiply } from "./math.ts";

console.log(add(1, 2))
console.log(multiply(1, 2))

We reexport the items in math.ts and import it in index.ts .

Fetch Data

We can fetch data with the Fetch API with Deno.

The Fetch API is implemented with Deno.

For example, we can write:

const res = await fetch("https://yesno.wtf/api");
const json = await res.json()
console.log(json);

Then when we run:

deno run --allow-net index.ts

We get something like:

{
  answer: "yes",
  forced: false,
  image: "https://yesno.wtf/assets/yes/7-653c8ee5d3a6bbafd759142c9c18d76c.gif"
}

logged.

The --allow-net flag enables network communication in our script.

To get HTML, we can write:

const res = await fetch("https://deno.land/");
const html = await res.text()
console.log(html);

Then we download the HTML code from the deno.land URL.

To catch errors, we write:

try {
  await fetch("https://does.not.exist/");
} catch (error) {
  console.log(error.message)
}

Then we log the message from the error.message property.

Read and Write Files

Deno comes with an API for reading and writing files.

They come with both sync and async versions.

The --allow-read and --allow-write permissions are required to gain access to the file system.

For example, we can write:

index.ts

const text = await Deno.readTextFile("./people.json");
console.log(text)

people.json

[
  {
    "id": 1,
    "name": "John",
    "age": 23
  },
  {
    "id": 2,
    "name": "Sandra",
    "age": 51
  },
  {
    "id": 5,
    "name": "Devika",
    "age": 11
  }
]

Then when we run:

deno run --allow-read index.ts

We read the JSON file and log the data.

Deno.readTextFile returns a promise so we can use await .

Writing a Text File

To write a text file, we can use the writeTextFile method.

For example, we can write:

await Deno.writeTextFile("./hello.txt", "Hello World");
console.log('success')

to create hello.txt and use the string in the 2nd argument as the content.

Then when we run:

deno run --allow-write index.ts

We can also use the writeTextFileSync method to write text file synchronously.

For example, we can write:

try {
  Deno.writeTextFileSync("./hello.txt", "Hello World");
  console.log("Written to hello.txt");
} catch (e) {
  console.log(e.message);
}

Read Multiple Files

We can read multiple files by writing:

for (const filename of Deno.args) {
  const file = await Deno.open(filename);
  await Deno.copy(file, Deno.stdout);
  file.close();
}

Then when we run:”

deno run --allow-read index.ts foo.txt bar.txt

We should see the contents of foo.txt and bar.txt printed.

We call Deno.open to open each file.

Deno.args has the command line arguments, which is ['foo.txt', 'bar.txt'] .

And then we call Deno.copy to copy the content to stdout .

And then we close the filehandle with file.close .

Conclusion

We can make HTTP requests with the fetch API and read and write files with Deno.

Leave a Reply

Your email address will not be published. Required fields are marked *