Getting started with Deno

Getting started with Deno

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
Things Deno offers out of the box

Installing Deno.

There are few options available on official site 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

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

Hello World - Writing first program with Deno.

Open up your terminal and just type following:

deno https://deno.land/std/examples/welcome.ts

will result into:

Executing a remote .ts file

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

Content of https://deno.land/std/examples/welcome.ts

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

Now using terminal execute following command:

deno index.ts

or

deno index.js
Hello World!! with index.ts
Hello World!! with index.js

Setting up an http-server.

Deno provides an http-server i.e.

https://deno.land/[email protected]/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/[email protected]/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

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

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";

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/");
Here we are spinning of an http server with Oak

Result:

GET request on server running on localhost:5000
POST request on server running on localhost:5000

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

Deno Examples: https://deno.land/#example

Oak Middleware: https://github.com/oakserver/oak


Original article on: https://blog.kushalbhalaik.xyz/getting-started-with-deno/