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:


export {
} from "";


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("");
const json = await res.json()

Then when we run:

deno run --allow-net index.ts

We get something like:

  answer: "yes",
  forced: false,
  image: ""


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

To get HTML, we can write:

const res = await fetch("");
const html = await res.text()

Then we download the HTML code from the URL.

To catch errors, we write:

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

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:


const text = await Deno.readTextFile("./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");

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) {

Read Multiple Files

We can read multiple files by writing:

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

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 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 .


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 *