Getting started with Deno
Tags: deno, oakserver, node
Published: 2020-02-15 14:09:25

---

firstPublishedAt: 1581775302365
latestPublishedAt: 1581775302365
slug: getting-started-with-deno
title: Getting started with Deno

---

![Deno logo on https://deno.land](https://cdn-images-1.medium.com/max/2216/1*IyVGV5RGsrwkEc6z_hf_IQ.png)

In this article, I’m going to show you how to get started with Deno — A secure runtime for JavaScript and TypeScript. We’ll see Installation of Deno, Hello World program and setting up an http-server.

---

# The What?

Deno is a secure runtime for Javascript & Typescript (right out of the box) just like Nodejs is a runtime for Javascript. Deno was created by same guy who created Nodejs i.e. Ryan Dahl.

Deno aims to improve what Ryan Dahl thinks he should’ve done with Nodejs like:

![**Deno aims to improve what’s wrong with Nodejs**](https://cdn-images-1.medium.com/max/1448/0*u2FD3UvbYsgOJ1Lm.png)

![**Things Deno offers out of the box**](https://cdn-images-1.medium.com/max/1654/0*VVAL5csJzmPIeOts.png)

# Installing Deno.

There are few options available on official site [deno.land/](https://deno.land/).

We are gonna install it using Power-shell command:

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

![**Message after Deno is successfully installed**](https://cdn-images-1.medium.com/max/1716/0*yOGxGiwF0K2DXh_2.png)

The Deno.exe executable file is stored in _C:\Users\<username>\.deno\bin\deno.exe_ by default.

In macOS or Linux _$HOME/.local/bin_

Following command will give info about Deno, V8 Engine & Typescript version installed on your machine.

```
`deno --version`
```

![**Deno versions**](https://cdn-images-1.medium.com/max/1952/0*OyhpdNVACgsbpbb4.png)

# Hello World — Writing first program with Deno.

Open up your terminal and just type following:

```
`deno [https://deno.land/std/examples/welcome.ts](https://deno.land/std/examples/welcome.ts)`
```

will result into:

![**Executing a remote .ts file**](https://cdn-images-1.medium.com/max/1954/0*S10fbySzlSsJqWnp.png)

What happened here is we execute a code present in a remote file, AWESOME!!

![**Content of [https://deno.land/std/examples/welcome.ts](https://deno.land/std/examples/welcome.ts)**](https://cdn-images-1.medium.com/max/1928/0*0pld_x7LauVJV2lG.png)

Now let’s execute a local code:

Create a file inside “C:\deno” > index.ts (it could have .js extension for a javascript file)

![**Opening index.ts in Visual Studio Code**](https://cdn-images-1.medium.com/max/2216/0*ZQl60kpAWMpkJcOl.png)

Now using terminal execute following command:

```
`deno index.ts`

`or`

`deno index.js`
```

![**Hello World!! with index.ts**](https://cdn-images-1.medium.com/max/1956/0*WV0aKKlghJblNd7h.png)

![**Hello World!! with index.js**](https://cdn-images-1.medium.com/max/1950/0*hkZqpSCOZAObfWnK.png)

# Setting up an http-server.

Deno provides an http-server i.e.

```
`[https://deno.land/std@v0.30.0/http/server.ts](https://deno.land/std@v0.30.0/http/server.ts)`
```

As provided on official website example, here’s how you can create a running server on your machine:

**Code:**

```
`import { serve } from "https://deno.land/std@v0.30.0/http/server.ts";`

`const s = serve({ port: 5000 });`

`console.log("Listening on http://localhost:5000/");`

`for await (const req of s) {
  req.respond({ body: "Hello World!!" });
}`
```

just copy paste above code into your **_“index.ts”_** file and run it with following command.

```
`deno -A index.ts`
```

> _-A flag provides all the necessary permission for your app to run on your machine_

![**GET request on server running on localhost:5000**](https://cdn-images-1.medium.com/max/2804/0*4FmvvxAYH6pg7ffh.png)

# Using Oak middle-ware with deno http server.

Oak is a middleware framework for Deno’s net server, more on this can be found on GitHub repo: [https://github.com/oakserver/oak](https://github.com/oakserver/oak)

NOW let’s create a GET & POST endpoint by altering our **_“index.ts”_** code with following:

```
import { Application, Router } from "[https://deno.land/x/oak/mod.ts](https://deno.land/x/oak/mod.ts)";

const router = new Router();

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

router.post("/", context => {
  context.response.body = "You have made a POST request!";
});

const app = new Application();

app.use(router.routes());
app.use(router.allowedMethods());

const server = app.listen({ port: 5000 });

console.log("Listening on [http://localhost:5000/](http://localhost:5000/)");
```

**Result:**

![**GET request on server running on localhost:5000**](https://cdn-images-1.medium.com/max/2804/0*eOYUPJH2BWf-HTFl.png)

![**POST request on server running on localhost:5000**](https://cdn-images-1.medium.com/max/3840/0*6Qz7Ngh3PxAc6J9W.png)

So, that was it regarding installing Deno, writing first ‘Hello-World” program and setting up an http-server along with Oak middleware.

**Resources:**
Deno — a better Node.js? | Krzysztof Piechowicz : [https://www.youtube.com/watch?v=mzfw9TwBiQc&t=616s](https://www.youtube.com/watch?v=mzfw9TwBiQc&t=616s)

Deno Examples: [https://deno.land/#example](https://deno.land/#example)

Oak Middleware: [https://github.com/oakserver/oak](https://github.com/oakserver/oak)

**Original article on**: [https://blog.kushalbhalaik.xyz/getting-started-with-deno/](https://blog.kushalbhalaik.xyz/getting-started-with-deno/)