[go: up one dir, main page]

DEV Community

Kushal
Kushal

Posted on • Edited on

Getting started with Deno

Deno logo on https://deno.land

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`
Enter fullscreen mode Exit fullscreen mode

**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`
Enter fullscreen mode Exit fullscreen mode

**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](https://deno.land/std/examples/welcome.ts)`
Enter fullscreen mode Exit fullscreen mode

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](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`
Enter fullscreen mode Exit fullscreen mode

**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/std@v0.30.0/http/server.ts](https://deno.land/std@v0.30.0/http/server.ts)`
Enter fullscreen mode Exit fullscreen mode

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!!" });
}`
Enter fullscreen mode Exit fullscreen mode

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

`deno -A index.ts`
Enter fullscreen mode Exit fullscreen mode

-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](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/)");
Enter fullscreen mode Exit fullscreen mode

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/

Top comments (0)