DEV Community

loading...

Deno — HTTP Requests and File Manipulation

aumayeung profile image John Au-Yeung Originally published at thewebdev.info ・3 min read

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

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";
Enter fullscreen mode Exit fullscreen mode

index.ts

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

console.log(add(1, 2))
console.log(multiply(1, 2))
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Then when we run:

deno run --allow-net index.ts
Enter fullscreen mode Exit fullscreen mode

We get something like:

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

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);
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

people.json

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

Then when we run:

deno run --allow-read index.ts
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

Then when we run:”

deno run --allow-read index.ts foo.txt bar.txt
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)

pic
Editor guide