Deno Web Server

Using Deno as a web server is a simple and secure task.

Deno can be used to create a server that serves just a simple text, and the one that reads from a file and serve HTML.

Deno Create a Simple Server

The following example creates a web server and writes to the browser whenever a user visits the URL.

  • Create a new project folder called my_server
  • Create a file server.js inside your new project folder
  • Navigate to your project from the terminal or open it in your favorite editor.

Example 1

//Import the server module
import { serve } from "https://deno.land/std/http/server.ts";

// serve on port 5000
const s = serve({ port: 5000 });

console.log('Listening to port 5000 on http://localhost:5000');

// Wait for request and response with a text
for await (const req of s) {
  req.respond({ body: "My First Deno Web Server\n" });
}
  Do It Yourself
a

Example Explained

  • The first part of the code imports a module to serve the web.
  • The second part of the code usesĀ serve() method to create the connection through port 5000
  • The third part of the code store the port in a variable
  • The last part loops through a promise to send the string "My First Deno Web Server"
Importing modules will be treated in the next chapters.

Run the Code

In your terminal run:

deno run --allow-net server.js

Visit localhost:5000 to test the server.

For security reasons: Deno does not allow programs to access the network without the --allow-net flag.

Deno Serve HTML File

To serve an HTML file and display it in a browser.

  • In the same project folder, create a file called web_server.js
  • In the same folder, create a new file index.html, this will be our HTML file to serve.

Example 1

//Import the server module
import { serve } from "https://deno.land/std/http/server.ts";

// serve on port 5000
const s = serve({ port: 5000 });

console.log('Listening to port 5000 on http://localhost:5000');

// Wait for request and response with a text
for await (const req of s) {

  // Read the index.html file and serve it
  const html_file = await Deno.readFile('./index.html');

  // create an decoder which decodes your html file to strings before sending
  const decoder = new TextDecoder()
  req.respond({ body: decoder.decode(html_file) });
}
  Do It Yourself

Run the program on your terminal with:

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

Example Explained

  • The first part of the code stores the hostname in a variable
  • The second part of the code store the port in a variable
  • The last part loops through the request promise, open the created index.html with Deno.open() and then sends it a string to the response.

Run the Code

In your terminal run:

deno run --allow-net web_server.js

For more examples on Deno Web Server, visit our Deno examples page below:

Deno Examples Page

You will learn about how to make 'HTTP Requests' in Deno in the 'Next Chapter'

What You Should Know at the End of This Chapter

  • You should be able to create a simple webserver with Deno.
  • You should be able to serve an HTML file with Deno.
  • You should be able to run the Deno web server and understand the function of the --allow-net flag.