Categories
Deno

Getting Started with Writing Server-Side Apps for the Deno Runtime Environment

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.

Installation

Deno ships as a single executable with no dependencies.

So we can install them easily.

It’s available on multiple platforms.

We can run the following commands to install them:

Shell (Mac, Linux):

$ curl -fsSL https://deno.land/x/install/install.sh | sh

PowerShell (Windows):

$ iwr https://deno.land/x/install/install.ps1 -useb | iex

Homebrew (Mac):

$ brew install deno

Chocolatey (Windows):

$ choco install deno

Scoop (Windows):

$ scoop install deno

Build and install from source using Cargo

$ cargo install deno

Getting Started

To create a hello world app, we can write:

console.log("hello world");

console is available within the Deno runtime.

We can create a simple server app by writing:

import { serve } from "https://deno.land/std@0.75.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello Worldn" });
}

We import the server module directly from the URL.

Then we call serve to create our server.

And we listen for requests by looping through the s array and send our response with req.respond .

Now we should see ‘Hello world’ on the browser screen.

Making an HTTP Request

We can make an HTTP request by writing:

index.ts

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

Then we go into our project and run it by running:

deno run --allow-net index.ts https://yesno.wtf/api

The --allow-net option is required to access networks.

Deno.args lets us get the command line arguments.

fetch lets us make the HTTP request with the given url .

And we get the request by call res.arrayBuffer to parse the body.

And then we write the response to the screen with Deno.stdout.write .

Reading a File

Deno comes with methods to read a file right out of the box.

To do this, we write:

index.ts

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

We get the file paths from Deno.args .

Then we loop through the filenames and call Deno.open to open the files.

Then we call Deno.copy to copy the file content to stdout to display them on the screen.

When we’re done, we call file.close to close the filehandle.

Now when we run;

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

And assuming files/bar.txt and files/foo.txt exist, then we should see the content of each file displayed.

The --allow-read flag lets us enable permission to read files from the file system.

Conclusion

We can create simple server-side JavaScript or TypeScript apps and run them with the Deno runtime.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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