Getting Started with Deno: Building HTTP and File Servers
Written on
Introduction to Deno
Deno is an innovative runtime environment designed for executing JavaScript and TypeScript applications on the server side. This guide will provide a comprehensive overview of how to initiate app development with Deno.
Creating a Simple HTTP Web Server
To set up a basic HTTP server using Deno's server module, you can utilize the following code snippet:
const server = serve({ hostname: "0.0.0.0", 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 });
}
In this example, we import the serve function to establish our HTTP server. The server listens for requests on the specified hostname and port. Within a for-await-of loop, we construct the response body by retrieving the user-agent from the request headers. We then send the response using the request.respond method.
To execute the script, run:
deno run --allow-net index.ts
Afterward, navigating to http://localhost:8080 should display:
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
Creating a File Server
Setting up a file server with Deno is straightforward. You can achieve this by executing the following command:
Once running, visiting http://localhost:4507/ will reveal a list of files from the current directory.
Building a TCP Echo Server
You can also create a TCP echo server using the Deno.listen and Deno.copy methods. Here’s how:
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
Deno.copy(conn, conn);
}
This code listens on port 8080 and loops through incoming connections, echoing data back to the client.
Creating a Subprocess
Deno allows you to create subprocesses with the Deno.run method. For instance, to execute the command echo hello, you can use the following code:
const p = Deno.run({
cmd: ["echo", "hello"],
});
await p.status();
This command runs the echo statement and waits for it to finish. To allow subprocesses, ensure to include the --allow-run flag.
You can also manage subprocesses and handle their output. For example:
const fileNames = Deno.args;
const p = Deno.run({
cmd: [
"deno",
"run",
"--allow-read",
...fileNames,
],
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);
console.log(errorString);
}
Deno.exit(code);
This example retrieves the status code of the subprocess, outputs the result if successful, and logs any errors if they occur.
Conclusion
With Deno, creating a simple HTTP server, a file server, and managing subprocesses is remarkably easy.
If you found this article helpful, consider subscribing to our YouTube channel for more content like this!