Robust Web App Development with Deno: A Step by Step Guide published 9/29/2023 | 3 min read

This article was ai-generated by GPT-4 (including the image by Dall.E)!
Since 2022 and until today we use AI exclusively (GPT-3 until first half of 2023) to write articles on devspedia.com!

Deno: Shaping the Future of Web App Development?

Deno is an exciting runtime for JavaScript and TypeScript that brings a fresh perspective to scripting in the modern web. Launched by Node.js founder Ryan Dahl, Deno is out to address what Dahl considers are Node.js's most noticeable shortcomings. With built-in TypeScript support, a focus on security, and the flexibility to import modules from URLs, Deno promises to make web app development an easier, more efficient, and secure process.

This article will guide you on how to leverage Deno's features to build robust web apps. We will also explore how Deno's philosophy holds up in practice and what sets it apart from its predecessor, Node.js.

Preparing Your Development Environment

To start building your app with Deno, first, you must install the Deno runtime. Note that Deno supports macOS, Windows, and Linux. You can download it and get it up and running using the commands designated for your operating system found at deno.land.

Once you've installed Deno, verify the installation by running the following command in your terminal:

  
deno --version

You should see the version of Deno you've installed printed out in the terminal.

Building a Simple Deno Web Server

Now that we have Deno installed, we can build a simple Deno web server as a preliminary step.

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

const server = serve({ port: 8000});

console.log('HTTP webserver running on: http://localhost:8000');

for await (const request of server) {
  request.respond({body: "Hello, World!
"});
}

To run the web server, use the following command in your terminal:

  
deno run --allow-net server.ts

This command tells Deno to execute your server.ts file and allow network access, a requirement for serving HTTP responses.



Setting Up a Routing System

For a more complex web app, we need to set up a routing system. Deno provides a simple and efficient way to create routes using oak, a middleware framework for Deno's server-side application. To set it up, change your server.ts code to the following:

  
// server.ts

import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();
router
  .get("/", (context) => {
    context.response.body = "Hello, World!";
  })
  .get("/deno", (context) => {
    context.response.body = "Welcome to Deno";
  });

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

console.log(`HTTP webserver running on: http://localhost:8000`);
await app.listen({ port: 8000 });

You can now access the routes by visiting http://localhost:8000 and http://localhost:8000/deno.

Decoding the Benefits of Deno

Finally, let's explore why developers might choose Deno over the long-established Node.js. Here are the key features that differentiate Deno:

  1. Improved Security: Deno puts security at the forefront. It operates in a secure sandbox and restricts file, network, and environment access, unless explicitly enabled.

  2. Built-In TypeScript Support: Deno supports TypeScript out of the box, which makes it possible to develop robust, self-documented JavaScript applications easier.

  3. Standard Library: Just like Go and Python, Deno provides a standard library that includes utilities to interact with the date, HTTP, and even testing frameworks.

While Deno is relatively new and has a long road ahead, it offers an exciting world of possibilities for modern web app development. By understanding Deno's principles and putting them into practice, we can get a taste of the future of web app development today.



You may also like reading: