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.
Create file locally called first_steps.ts and copy and paste the code line
below:
console.log("Welcome to Deno!");Running Deno Programs
Now to run the program from the terminal:
deno run first_steps.tsDeno also has the ability to execute scripts from URLs. Deno
hosts a library of example code, one
of which is a Hello World program. To run that hosted code, do:
deno run https://deno.land/std@0.103.0/examples/welcome.tsMaking 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 terminal. Just
like in the browser you can use the web standard
fetch API to
make HTTP calls.
In the first_steps.ts file you created above, paste the code below:
const url = Deno.args[0];
const res = await fetch(url);
const body = new Uint8Array(await res.arrayBuffer());
await Deno.stdout.write(body);Let's walk through what this application does:
- We get the first argument passed to the application, and store it in the
urlconstant. - We make a request to the url specified, await the response, and store it in
the
resconstant. - We parse the response body as an
ArrayBuffer, await the response, and convert it into aUint8Arrayto store in thebodyconstant. - We write the contents of the
bodyconstant tostdout.
Try it out:
deno run first_steps.ts https://yirenlu.com/or, from URL:
deno run https://deno.land/std@0.167.0/examples/curl.ts https://example.comYou 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:
deno run --allow-net=yirenlu.com first_steps.ts https://yirenlu.com/or, from URL:
deno run --allow-net=example.com https://deno.land/std@0.167.0/examples/curl.ts https://example.comReading 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 built-in APIs here at
/api.
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.
import { copy } from "https://deno.land/std@0.167.0/streams/conversion.ts";
const filenames = Deno.args;
for (const filename of filenames) {
const file = await Deno.open(filename);
await copy(file, Deno.stdout);
file.close();
}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.
Again, here, we need to give --allow-read access to the program.
Try the program:
# macOS / Linux
deno run --allow-read https://deno.land/std@0.167.0/examples/cat.ts /etc/hosts
# Windows
deno run --allow-read https://deno.land/std@0.167.0/examples/cat.ts "C:\Windows\System32\Drivers\etc\hosts"Putting it all together in an HTTP server
One of the most common usecases for Deno is building an HTTP Server.
http_server.ts
import { serve } from "https://deno.land/std@0.157.0/http/server.ts";
const port = 8080;
const handler = async (request: Request): Promise<Response> => {
const resp = await fetch("https://api.github.com/users/denoland", {
// The init object here has an headers object containing a
// header that indicates what type of response we accept.
// We're not specifying the method field since by default
// fetch makes a GET request.
headers: {
accept: "application/json",
},
});
return new Response(resp.body, {
status: resp.status,
headers: {
"content-type": "application/json",
},
});
};
console.log("Listening on http://localhost:8000");
serve(handler);Let's walk through what this program does.
- Import the http server from
std/http(standard library) - Import a third party module,
url_join, which is hosted at deno.land/x. (Since it can be unwieldy to import URLs everywhere, best practice is actually to import and re-export your external libraries into a centraldeps.tsfile. For more details read here). - HTTP servers need a handler function. This function is called for every
request that comes in. It must return a
Response. The handler function can be asynchronous (it may return aPromise). - In the handler function, use
url_jointo join together a complex Github url. - Use
fetchto fetch the url. - Write the response body to a local file.
- Return the Github response as a response to the handler.
- Finally, to start the server on the default port, call
servewith the handler.
Now run the server. Note that you need to give both network and write permissions.
deno run --allow-net --allow-write http_server.tsWith the server listening on port 8000, make a GET request to that endpoint.
curl http://localhost:8000You will see a JSON response from the Deno Github page.
More examples
You can find more examples in the Examples chapter and at Deno by Example.