Skip to main content

Creating a Web Application using Deno

Simple Web Application

So without wasting time lets jump into the code and write a simple hello world program using the abcmodule.

Create a file index.ts, and copy the code there.

import { Application } from "https://deno.land/x/abc@v1.0.0-rc10/mod.ts";

const app = new Application();

app
  .get("/hello", (c) => {
    return "Hello, Abc!";
  })
  .start({ port: 8000 });

After saving the file you can run the code using the below cmd

deno run --allow-net=0.0.0.0:8000 index.ts

Explanation: Deno is secure by default. Therefore, unless you specifically enable it, a deno module has no file, network, or environment access for example. Access to security-sensitive areas or functions requires the use of permissions to be granted to a deno process on the command line.

--allow-net=\<allow-net> Allow network access. You can specify an optional, comma-separated list of domains to provide a whitelist of allowed domains. For more information on the different kind of permissions needed in the deno you can visit Permissions in Demo

Now once you run the above cmd you can see the Hello, Abc! in the browser by redirecting to http://localhost:8000/hello

Lets Go Deeper!!!

Now let's try to serve the static file using the abc module. In the above example, app will have lots of different methods that we can use, one of them is static(), it used to serve the static files.

Let us create a folder called public in the root of the project. And inside that create an index.htmlfile. Your index.html will look something like this.

<!doctype html>
<html>
    <head>
        <title>Deno Web Application</title>
    </head>
    
    <body>
    <div>
        <h1>Deno Web Application</h1>
    </div>
    </body>
</html>

After creating this file let's modify our index.tsx file by adding the below code.

import { Application } from "https://deno.land/x/abc@v1.0.0-rc10/mod.ts";

const app = new Application();

app
  .get("/hello", (c) => {
    return "Hello World"
  })
  .static("/", "./public")
  .start({ port: 8000 });

Explanation: As you can see .static("/", "./public") we have added this route / and we are serving our static file using the static() method provided by the abc module.

static() registers a new route with path prefix to serve static files from the provided root directory.

Let's try to run this using the previous cmd that is

deno run --allow-net=0.0.0.0:8000 index.ts

Oops!! once you try to run this it will compile successfully but when you visit this pathhttp://localhost:8000/index.html, you will get below error in the browser.

{"statusCode":500,"error":"Internal Server Error","message":"read access to <CWD>, run again with the --allow-read flag"}

It's because as we know deno requires the permission to read the file from the system. Hence to run the above cmd with --allow-read flag will solve our problem. So let's use this cmd to compile our index file.

deno run --allow-read --allow-net=0.0.0.0:8000 index.ts

After successful compile, you will see the output as below.

deno-first-app

Creating a basic calculator app

Hence after the successful serving of our static file, we can also add js in it. Let start with the basic calculator app for now.

Firstly lets modify our index.html file and add the below code

<html>
  <head>
    <script src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <!-- create table -->
  <body>
    <div class="title">Deno Calculator App</div>
    <table border="1">
      <tr>
        <td colspan="3"><input type="text" id="result" /></td>
        <!-- clr() function will call clr to clear all value -->
        <td><input type="button" value="c" onclick="clr()" /></td>
      </tr>
      <tr>
        <!-- create button and assign value to each button -->
        <!-- dis("1") will call function dis to display value -->
        <td><input type="button" value="1" onclick="dis('1')" /></td>
        <td><input type="button" value="2" onclick="dis('2')" /></td>
        <td><input type="button" value="3" onclick="dis('3')" /></td>
        <td><input type="button" value="/" onclick="dis('/')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="4" onclick="dis('4')" /></td>
        <td><input type="button" value="5" onclick="dis('5')" /></td>
        <td><input type="button" value="6" onclick="dis('6')" /></td>
        <td><input type="button" value="-" onclick="dis('-')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="7" onclick="dis('7')" /></td>
        <td><input type="button" value="8" onclick="dis('8')" /></td>
        <td><input type="button" value="9" onclick="dis('9')" /></td>
        <td><input type="button" value="+" onclick="dis('+')" /></td>
      </tr>
      <tr>
        <td><input type="button" value="." onclick="dis('.')" /></td>
        <td><input type="button" value="0" onclick="dis('0')" /></td>
        <!-- solve function call function solve to evaluate value -->
        <td><input type="button" value="=" onclick="solve()" /></td>
        <td><input type="button" value="*" onclick="dis('*')" /></td>
      </tr>
    </table>
  </body>
</html>

As you can see the above code we have already imported our js and css inside to so lets create JS file called script.js and add the following functions in it.

//function that display value
function dis(val) {
  document.getElementById("result").value += val;
}

//function that evaluates the digit and return result
function solve() {
  let x = document.getElementById("result").value;
  let y = eval(x);
  document.getElementById("result").value = y;
}

//function that clear the display
function clr() {
  document.getElementById("result").value = "";
}

And also add some css into it by creating style.css file.

.title {
  margin-bottom: 10px;
  text-align: center;
  width: 210px;
  color: #0d8dcf;
  border: solid black 2px;
}

input[type="button"] {
  background-color: #0d8dcf;
  color: black;
  border: solid black 2px;
  width: 100%;
}

input[type="text"] {
  background-color: white;
  border: solid black 2px;
  width: 100%;
}

Great!!! Our calculator app is now almost ready, let's save all the file and reload the http://localhost:8000/index.html.

You can see our calculator app in the browser like below.


Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...