SEO AGENCY IN MUMBAI

The most effective way to ensure that your website actually ranks for keyword searches that are relevant to your organization or website is to hire an SEO company in Mumbai. You can enhance your…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deno HTTP Server in 2 ways

HTTP server is the bread and butter of runtimes like Deno, Node.js, etc. Initially, Deno’s HTTP server was mostly a part of the standard library. Due to this, a large part of the implementation of the HTTP server was in TypeScript. The performance was good, but there was a tremendous scope for improvement.

To make it more efficient, Deno enhanced by using Rust’s hyper HTTP server and moved all the HTTP server code into the core runtime. Recently, Deno has made a stable release of the native HTTP server (hyper based). The standard library’s HTTP server is also updated to use native HTTP server internally.

In this article, we’ll go over the two ways of implementing an HTTP server in Deno:

The first style is to use iterators to process everything, like:

Here is a simple ‘Hello world’ HTTP server that uses iterators:

For efficiency, a different function must be used for handling incoming TCP connections (handleNewConnection), otherwise the HTTP server would freeze waiting for the current connection iteration to finish up.

There are two iterators in the above code:

The above code can be rewritten using while loops instead of async iterators. This time we’ll use an async function called: nextRequest. The code is more or less the same. There are while loops instead of for loops. In case a single request would be only be handled, the second while loop can be dropped.

The code looks like this for a single request handling:

That’s all about the iterator based HTTP server. Let’s move on to callback style.

The second way to write an HTTP server is to use the callback style provided by the standard library’s HTTP module. The callback style is similar to Node.js’s style of writing a native HTTP server.

First import a function called serve:

In callback style, a callback function would get invoked for each request. The callback handler must return a response object. The number of lines of code is very less when compared to iterator based HTTP server.

The general usage is:

In fact, a ‘Hello world’ HTTP server can be written in a single line of code:

That’s it! A single of code starts the listener, accepts incoming connections, serves HTTP.

Now that we’ve seen both the styles, let’s write an echo server in both styles. Note that, in both cases, the body of the Request object is given directly to the Response object.

First, an echo server in iterator style:

Here is the output of a sample run:

Second, an echo server in callback style:

Here is the output of a sample run:

For further reading, here are a couple of resources to understand Deno’s HTTP server in detail:

Add a comment

Related posts:

How To Deliver A Successful Project Execution Plan

A groundbreaking idea is what all the greatest businesses are made of. Yet, there is a sidelined element to this story. Behind the scenes, there’s one thing that makes it possible for your idea to…

Should we replicate the human mind?

Who does not remember the poignant film “Bicentennial man” [1], with Robin Williams in the role of a positronic robot bought by a family, who gradually shows curiosity, learning skills and human…

Fly

It can be hard for one to fit in with other members of today’s society. In fact, people have been influenced so much by popular culture that a majority of citizens believe that certain materialistic…