First steps

This page contains some examples to teach you about the fundamentals of Deno.

This document assumes that you have some prior knowledge of JavaScript, especially about async/await. If you have no prior knowledge of JavaScript, you might want to follow a guide on the basics of JavaScript before attempting to start with Deno.

Hello World

Deno is a runtime for JavaScript/TypeScript which tries to be web compatible and use modern features wherever possible.

Browser compatibility means a Hello World program in Deno is the same as the one you can run in the browser:

  1. console.log("Welcome to Deno!");

Try the program:

  1. deno run https://deno.land/std@$STD_VERSION/examples/welcome.ts

Making an HTTP request

Many programs use HTTP requests to fetch data from a webserver. Let’s write a small program that fetches a file and prints its contents out to the terminal.

Just like in the browser you can use the web standard fetch API to make HTTP calls:

  1. const url = Deno.args[0];
  2. const res = await fetch(url);
  3. const body = new Uint8Array(await res.arrayBuffer());
  4. await Deno.stdout.write(body);

Let’s walk through what this application does:

  1. We get the first argument passed to the application, and store it in the url constant.
  2. We make a request to the url specified, await the response, and store it in the res constant.
  3. We parse the response body as an ArrayBuffer, await the response, and convert it into a Uint8Array to store in the body constant.
  4. We write the contents of the body constant to stdout.

Try it out:

  1. deno run https://deno.land/std@$STD_VERSION/examples/curl.ts https://example.com

You will see this program returns an error regarding network access, so what did we do wrong? You might remember from the introduction that Deno is a runtime which is secure by default. This means you need to explicitly give programs the permission to do certain ‘privileged’ actions, such as access the network.

Try it out again with the correct permission flag:

  1. deno run --allow-net=example.com https://deno.land/std@$STD_VERSION/examples/curl.ts https://example.com

Reading a file

Deno also provides APIs which do not come from the web. These are all contained in the Deno global. You can find documentation for these APIs on doc.deno.land.

Filesystem APIs for example do not have a web standard form, so Deno provides its own API.

In this program each command-line argument is assumed to be a filename, the file is opened, and printed to stdout.

  1. import { copy } from "https://deno.land/std@$STD_VERSION/streams/conversion.ts";
  2. const filenames = Deno.args;
  3. for (const filename of filenames) {
  4. const file = await Deno.open(filename);
  5. await copy(file, Deno.stdout);
  6. file.close();
  7. }

The copy() function here actually makes no more than the necessary kernel→userspace→kernel copies. That is, the same memory from which data is read from the file, is written to stdout. This illustrates a general design goal for I/O streams in Deno.

Try the program:

  1. # macOS / Linux
  2. deno run --allow-read https://deno.land/std@$STD_VERSION/examples/cat.ts /etc/hosts
  3. # Windows
  4. deno run --allow-read https://deno.land/std@$STD_VERSION/examples/cat.ts "C:\Windows\System32\Drivers\etc\hosts"

TCP server

This is an example of a server which accepts connections on port 8080, and returns to the client anything it sends.

  1. import { copy } from "https://deno.land/std@$STD_VERSION/streams/conversion.ts";
  2. const hostname = "0.0.0.0";
  3. const port = 8080;
  4. const listener = Deno.listen({ hostname, port });
  5. console.log(`Listening on ${hostname}:${port}`);
  6. for await (const conn of listener) {
  7. copy(conn, conn);
  8. }

For security reasons, Deno does not allow programs to access the network without explicit permission. To allow accessing the network, use a command-line flag:

  1. deno run --allow-net https://deno.land/std@$STD_VERSION/examples/echo_server.ts

To test it, try sending data to it with netcat (or telnet on Windows):

Note for Windows users: netcat is not available on Windows. Instead you can use the built-in telnet client. The telnet client is disabled in Windows by default. It is easy to enable however: just follow the instructions on Microsoft TechNet

  1. # Note for Windows users: replace the `nc` below with `telnet`
  2. $ nc localhost 8080
  3. hello world
  4. hello world

Like the cat.ts example, the copy() function here also does not make unnecessary memory copies. It receives a packet from the kernel and sends it back, without further complexity.

More examples

You can find more examples, like an HTTP file server, in the Examples chapter.