Creating an HTTP Service in Deno

tl;dr

import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="106364745066203e23263e20">[email protected]</a>/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}

Deno is a new runtime for JavaScript and TypeScript created by Ryan Dahl , whose previous work includes creating Node.js .

Before we write our service, you need to install or otherwise have a way to execute code using the Deno runtime. I’m sure there are Docker images for the latter, I opted to install locally on a Windows 10 machine using the Powershell install script .

To get started, create a main.ts file and add the following code:

import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="196a6d7d596f29372a2f3729">[email protected]</a>/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}

Before we dive into the code, let’s run the script with deno main.ts .

You might see an error when trying to run the script:

Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag, info on issue here https://deno.land/std/manual.md

By default, Deno does not allow your code to access the network. From the Deno docs :

For security reasons, Deno does not allow programs to access the network without explicit permission.

We use the --allow-net flag to allow network access:

deno --allow-net main.ts

If all is well, Deno should write to the console that our new HTTP service is running:

http://localhost:8000/

You can send an HTTP GET request to our service to verify that it’s working. To do that, I’ll use cURL : curl localhost:8000 . If all is well, you’ll receive an HTTP response with “Hello World” in the response body.

Cool!

So, how does it work? Let’s take it line by line.

Line 1

import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="394a4d5d794f09170a0f1709">[email protected]</a>/http/server.ts";

Deno can import libraries directly from URLs and that’s what we’re doing here. In our case, we’re importing a function called serve from the file located at https://deno.land/ [email protected] /http/server.ts .

Line 2

const s = serve({ port: 8000 });

We’re now invoking the serve function that we imported on line 1. The serve function creates a new HTTP server and bids it to the port specified – in our case that’s 8000. You can inspect the serve function here .

Line 3

console.log("http://localhost:8000/");

This write writes out http://localhost:8000 to the console.

Line 4

for await (const req of s) {

Line 2 created a variable s and stored the result of the expression serve({ port: 8000 }); in it. Variable s is now an instance of a class called Server that implements AsyncIterable<ServerRequest> . You can read more about the Server class particulars in the Deno docs .

For our purposes, we just need to know that Line 4 opens a for..of iterator that allows us to respond to each HTTP request.

Line 5

req.respond({ body: "Hello World\n" });

This line is important – it runs each time our server receives an HTTP request! In fact, the whole body of the for..of iterator is executed each time our HTTP service receives an HTTP request. To give you some perspective, DuckDuckGo averaged 61 million HTTP requests a day in April. That means that whatever code you have on Line 5 would run 61 million times a day if your code were serving that traffic.

For fun, try changing the "Hello World\n" text to something else, maybe "Hello from the recent past!\n" . If you restart our service by killing and re-running the script you can use our cURL command to see your new text: curl localhost:8000 .

Line 6

}

This line closes the for..of iterator that we opened with Line 4.

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章