How to build a Deno environment

overview

I will explain how to build an environment for Deno, a runtime for JavaScript and TypeScript.

This article describes how to build a Deno environment using the following versions.

Deno v1.35.0

In addition, all the code created this time is posted on GitHub.

*** Deno is a scripting language runtime. If you don’t have Deno installed, download it from the official website and install it. ** Deno

How to build environment

Follow the steps below to build the Deno environment.

Create project directory

First, create a new directory to create the Deno project. Run the below command in command line

mkdir deno-project

Change to project directory

Go to your project directory. Please run the following command:

cd deno-project

After moving, move to the root directory of the project.

Creating an application

Create a Deno application. Create a new file (e.g. app.ts) inside your project directory and add the following code

app.ts

import { Server } from "https://deno.land/std@0.193.0/http/server.ts";
const port = 3000;

const handler = (request: Request) => {
  const body = "Hello, Deno!";

  return new Response(body, { status: 200 });
};

const server = new Server({ port, handler });
server.listenAndServe();
console.log("HTTP webserver running at: http://localhost:3000/");

In the above example, we are using the HTTP module from the Deno standard library to create a server and return Hello, Deno! as a response.

Start the server

Finally, start the Deno server. Please run the below command

deno run --allow-net app.ts

If the server starts successfully, you should see “HTTP webserver running. Access it at: http://localhost:3000/” in the console. Visit http://localhost:3000 in your browser to make sure your Deno application is working properly.

  • --allow-net is a flag to give network access to Deno. Deno provides a secure environment by default, so all security-related features, such as file, environment variable, and network access, require explicit permissions.