How to make a Simple http web server to get started using Deno

Jul 20th 2020 byLunaticsatoshi

First of all to start, we need to install Deno and this task is very easy for any operating system. See more at DenoLand.


After Installation you can use the command Line tools to navigate around with deno. Run deno help to get more information\

$ deno --help

This entry point can be a JavaScript (.js) file or TypeScript (.ts) file. But the great news is the possibility to use a URL that points to an app entry point.

Deno’s website provides us with some great examples, like these.

$ deno run https://deno.land/std/examples/welcome.ts
Download https://deno.land/std/examples/welcome.ts
Compile https://deno.land/std/examples/welcome.ts
Welcome to Deno 🦕

You can also run a program using a file

import { welcome } from 'https://github.com/jaquiel/deno-features/raw/master/std/welcome.ts'

welcome()

Now getting back to what we intended to do in the begining i.e to make a deno server. Now that you know te basics of handling deno the task becomes really simple.

  1. First Make a File named server.js
  2. Now import serve and websockets from denoland

    import { serve } from "https://deno.land/std/http/server.ts";
    
  3. Now setup the server using the code

    import { serve } from "https://deno.land/std/http/server.ts";
    const server = serve({ port: 8000 });
    console.log("http://localhost:8000/");
    for await (const req of server) {
    req.respond({ body: "Hello World\n" });
    }
    
  4. After that run the server file using the following command

    deno run --allow-net --allow-read server.js
    

    So why the long way to run a file in deno when compared to node. In Deno's documentation it is clearly defined that deno is a secure runtime for JavaScript and TypeScript. To access to security-sensitive areas or functions, you must use permissions on the command line. Thus we need to specify the --allow-net and --allow-read flags to run a deno program in your system.

You can find the basic documentaion regarding this on Deno Http

Now that you know the basics of deno we can now move to making a basic chat app using Deno but lets save that for the next time.

Share Post