Deno — HTTP, Server, File Server, and Subprocesses

Spread the love

Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.

In this article, we’ll take a look at how to get started with developing apps for Deno.

Simple HTTP Web Server

We can create a simple HTTP server with the server module provided by Deno.

For example, we can write:

import { serve } from "";

const server = serve({ hostname: "", port: 8080 });
console.log(`HTTP server running on port 8080`);

for await (const request of server) {
  const bodyContent = `Your user-agent is: ${request.headers.get("user-agent") || "Unknown"}`;
  request.respond({ status: 200, body: bodyContent });

We import the serve function to create our HTTP server.

The object has the hostname and port to listen to requests from.

Then we create the response body in the for-await-of loop.

We get the header by calling request.headers.get .

Then we call request.response with the HTTP status code and body in the object that we use as the argument of respond .

Then we can run the script with:

deno run --allow-net index.ts

Then when we go to http://localhost:8080, we should see something like:

Your user-agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36

File Server

We can also create a file server easily with Deno.

To do this, we just run:

deno run --allow-net --allow-read

Then when we go to http://localhost:4507/, we see the files in the current folder listed.

TCP Echo Server

We can create a TCP echo server easily with the Deno.listen and Deno.copy methods.

For example, we can write:

const listener = Deno.listen({ port: 8080 });
console.log("listening on");
for await (const conn of listener) {
  Deno.copy(conn, conn);

to listen to port 8080.

Then we loop through the listener array to watch for requests.

Then we call Deno.copy to redirect the connection data inbound.

Creating a Subprocess

We can create a subprocess by using the method.

For example, we can run:

const p ={
  cmd: ["echo", "hello"],
await p.status();

to run echo hello .

Then we can wait for its completion with the p.status method.

We need the --allow-run flag to let us run subprocesses.

We can get results from subprocesses and run with them by using various methods.

For instance, we can write:

const fileNames = Deno.args;

const p ={
  cmd: [
  stdout: "piped",
  stderr: "piped",

const { code } = await p.status();

if (code === 0) {
  const rawOutput = await p.output();
  await Deno.stdout.write(rawOutput);
} else {
  const rawError = await p.stderrOutput();
  const errorString = new TextDecoder().decode(rawError);


We call the status method to get the status code.

Then we write the output if the code is 0.

Otherwise, we output the error to the stderr .

And we log the string.

And finally, we call Deno.exit with the exit code of our command.


We can create a simple HTTP server, file server, and run subprocesses easily with Deno.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *