Deno, the secure runtime for JavaScript and TypeScript, has been gaining traction in the development community as a powerful alternative to Node.js. One of the key features of Deno is its built-in HTTP module, which provides a simple and efficient way to create HTTP servers and clients. In this article, we will take a deep dive into the Deno HTTP module and explore its features, benefits, and usage in various scenarios.
Deno is a secure runtime for JavaScript and TypeScript, created by Ryan Dahl, the original creator of Node.js. Deno addresses some of the shortcomings of Node.js, such as the security issues related to the global access of modules and the complexity of the module resolution system.
Some of the key features of Deno include:
The Deno HTTP module is part of the Deno standard library and can be imported using the following syntax:
import { serve, Server, serveTLS } from "https://deno.land/std@0.115.0/http/server.ts";
import { HttpClient } from "https://deno.land/std@0.115.0/http/http_client.ts";
The main components of the Deno HTTP module are:
serve
: A function to create an HTTP serverServer
: An HTTP server class providing event-based request handlingserveTLS
: A function to create an HTTPS serverHttpClient
: An HTTP client class for making HTTP requestsTo create a simple HTTP server using the Deno HTTP module, you can use the serve
function. Here’s an example of a basic HTTP server that listens on port 8000 and responds with “Hello, Deno HTTP!” to all incoming requests:
import { serve } from "https://deno.land/std@0.115.0/http/server.ts";
const server = serve({ port: 8000 });
console.log("HTTP server is running on http://localhost:8000");
for await (const request of server) {
request.respond({ body: "Hello, Deno HTTP!" });
}
To run this script, save it as server.ts
and execute the following command in your terminal:
deno run --allow-net server.ts
The Deno HTTP module also provides an HttpClient
class for making HTTP requests. The following example demonstrates how to make a simple GET request using the HttpClient
:
import { HttpClient } from "https://deno.land/std@0.115.0/http/http_client.ts";
const client = new HttpClient();
const response = await client.get("https://jsonplaceholder.typicode.com/todos/1");
console.log(`Status: ${response.status}`);
console.log("Headers:", response.headers);
console.log("Body:", await response.text());
The Deno HTTP module provides a flexible API for handling incoming requests and generating responses. The following example demonstrates how to create a basic route handler with different responses based on the request URL:
import { serve } from "https://deno.land/std@0.115.0/http/server.ts";
const server = serve({ port: 8000 });
for await (const request of server) {
const url = new URL(request.url);
switch (url.pathname) {
case "/":
request.respond({ body: "Welcome to the Deno HTTP server!" });
break;
case "/about":
request.respond({ body: "This is an example Deno HTTP server." });
break;
default:
request.respond({
status: 404,
body: "Page not found.",
});
break;
}
}
The Deno HTTP module supports streaming and file handling out of the box. The following example demonstrates how to serve a static file using the Deno readFile
function and the Response
class:
import { serve } from "https://deno.land/std@0.115.0/http/server.ts";
import{ readFile } from "https://deno.land/std@0.115.0/fs/mod.ts";
const server = serve({ port: 8000 });
for await (const request of server) {
const url = new URL(request.url);
if (url.pathname === "/static") {
try {
const file = await readFile("static/sample.txt");
request.respond({
status: 200,
headers: new Headers({ "Content-Type": "text/plain" }),
body: file,
});
} catch (error) {
request.respond({ status: 404, body: "File not found" });
}
} else {
request.respond({ status: 404, body: "Page not found" });
}
}
While the Deno HTTP module provides a solid foundation for building HTTP servers, the developer community has created several middleware and frameworks to further simplify the process. One such popular framework is Oak, which provides an expressive API inspired by Koa and Express.js.
Here’s an example of a simple Oak-based server:
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
const router = new Router();
router.get("/", (context) => {
context.response.body = "Welcome to the Oak-powered Deno HTTP server!";
});
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8000 });
The Deno HTTP module offers a powerful and flexible API for creating HTTP servers and clients. With built-in support for features like streaming, file handling, and secure TLS connections, the module is well-suited for modern web development.
As the Deno ecosystem continues to grow, developers can also leverage middleware and frameworks like Oak to further streamline the process of building web applications. With its secure-by-default design and robust standard library, Deno is an exciting platform for web developers to explore and adopt.