“Hello, world!” in Deno

Learn how to write a “Hello, world!” application in Deno.

Prerequisites

To get started with writing a “Hello, world!” application in Deno, we need the following:

  • Deno: It’s already been installed for this course.
  • An IDE or text editor: We’ll use the code widgets provided in the lessons.

Running our first code

In this section, we’ll run our first code, which is pretty simple. All you need to do is run the following command in the terminal below:

Press + to interact
deno run https://deno.land/std/examples/welcome.ts

The output will be “Welcome to Deno!” because the welcome.ts file only has a simple console.log defined.

Terminal 1
Terminal
Loading...

The command deno run takes a TypeScript (or JavaScript) file as its input and simply runs it. In the example below, we have main.ts, a simple TypeScript file. Let’s run this file using the deno run command to print “Hello, world!” to the console.

Press + to interact
console.log("Hello, world!");

More complex example

In this section, we’ll create a more complex “Hello, world!” example, where we’ll handle an HTTP request. Take a look at this example in the code widget below:

import { Application, Router } from "https://deno.land/x/oak@v10.5.1/mod.ts";

const router = new Router();

router.get("/", (ctx) => {
  ctx.response.body = "Hello, world!";
});

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000, secure: true, certFile: "/usercode/server.cert", keyFile: "private.key" });
Hello, world! HTTP application

Explanation

  • Line 1: We import the Application and Router modules, which are located in the oak library.

  • Lines 5–7: We define the response that needs to be sent when accessing the application.

  • Line 13: We instruct the server to listen to the port 8000.