Getting started with Deno
In this article, I’m going to show you how to get started with Deno — A secure run-time 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 run-time for Javascript & Typescript (right out of the box) just like Nodejs is a run-time 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 the official site deno.land/.
We are gonna install it using the 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
The following command will give info about Deno, V8 Engine & Typescript version installed on your machine.
deno --version
Hello World — Writing the first program with Deno.
Open up your terminal and just type the 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.tsordeno index.js
Setting up an HTTP-server.
Deno provides an HTTP-server i.e.
https://deno.land/std@v0.30.0/http/server.ts
As provided on the 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 the 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 the 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/");
Result:
So, that was it regarding installing Deno, writing the 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
Originally published at https://kushalbhalaik.xyz/blog on February 15, 2020.