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:
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
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
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:
What happened here is we execute a code present in a remote file, AWESOME!!
Now let's execute a local code:
Create a file inside "C:\deno" > index.ts (it could have .js extension for a javascript file)
Now using terminal execute following command:
deno index.ts
or
deno 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
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:
Result:
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/