Building a Hyper Fast Web Server with Deno

Rate this content
Bookmark

Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.

This workshop has been presented at JSNation Live 2021, check out the latest edition of this JavaScript Conference.

FAQ

Deno is a JavaScript and TypeScript runtime built on the V8 JavaScript engine and written in Rust. It is designed to be a secure environment for executing modern JavaScript and TypeScript code outside the web browser. Deno emphasizes security, supports top-level await, and treats TypeScript as a first-class citizen without needing a separate configuration.

Deno handles third-party modules by importing them directly via URLs, eliminating the need for a centralized package manager like npm. This approach allows developers to load modules directly from any location on the web, and Deno caches these modules locally for offline development and to speed up load times.

Deno is designed with security in mind. By default, Deno executes code in a secure sandbox, meaning scripts do not have access to the file system, network, or environment unless explicitly granted permissions. This approach minimizes the risk of malicious scripts causing harm to the user's system.

Deno and Node.js are both JavaScript runtimes built on the V8 engine, but Deno offers several differences aimed at addressing some of Node.js's limitations. Deno emphasizes security, does not use npm, and uses URLs for module imports. It also supports TypeScript natively. Deno is designed to be a more modern and secure alternative, potentially suitable for new projects that prioritize these features.

Rust plays a crucial role in Deno by providing the underlying system that enhances Deno's security and performance. Rust's memory safety guarantees and performance characteristics make Deno more secure and efficient compared to platforms not utilizing Rust. This integration allows Deno to execute operations at a low level with higher security and speed.

In Deno, modules are imported directly using URLs, which can point to any location on the web that hosts JavaScript or TypeScript code. To import a module, you use the standard ES module syntax but specify a URL instead of a local path or package name. Deno caches these modules after the first import to improve load times and allow offline development.

Deno provides several built-in libraries for handling HTTP requests, such as the 'std/http' module for basic HTTP server functionalities. For more advanced use cases, third-party frameworks like Oak are recommended. Oak provides a middleware framework similar to Express.js in Node, offering a robust set of features for building complex web applications.

Yes, Deno can execute TypeScript directly without the need for transpiling the code to JavaScript first. Deno treats TypeScript as a first-class citizen, providing built-in support for TypeScript execution, which simplifies development workflows by eliminating the need for additional tooling.

Matt Landers
Matt Landers
Will Johnston
Will Johnston
156 min
16 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Workshop introduces Beano and Hyper, a JavaScript runtime and a Rust library known for its fast HTTP implementation. The benefits of using Rust and Hyper over the standard TypeScript implementation of HTTP are discussed. Load tests show that Hyper is about 35% faster. The process of building a web framework called Hyperbole is explained, along with the implementation of request handling logic and middleware. The use of Deno for libraries and production is explored, and the importance of load testing and optimization is emphasized.

1. Introduction to Beano and Hyper

Short description:

Hello. I'm Matt Landers. We're going to build a hyper-fast web server with Beano. Beano is a JavaScript runtime that natively supports TypeScript. Hyper is a Rust library known for its correct and fast HTTP implementation. Excited about the Beano 1.9 integration.

Hello. I'm Matt Landers. I am head of DevRel at WP Engine, and I'm joined by… with Will Johnston. We're both doing this session together. We're going to be building something with Beano. So, we're going to build a hyper-fast web server with Beano.

What were you saying, Will? I said, hey, guys. Hey. Will, good morning. It's early for me. It's early for me. It's not… maybe not as early for Matt. And I don't know about the rest of everybody. It's early for me only because I'm on vacation, and I was… I drove 14 hours overnight. I have slept a little since then, but maybe a little delirious, so maybe everything won't go as smooth as it would have, but I'm excited to do this still.

I found a place with some good internet and should be… should be good to go. I'm really excited about Beano. Hopefully, everybody here has heard of Beano, but I'll just give a real brief intro. Beano is a JavaScript runtime that natively supports TypeScript, which is really awesome for me because I love TypeScript. So you can just create a TS file, run it with Beano, and you're good to go. You don't need a TS config or anything like that. It just works. Beano is built in Rust, which that is kind of a… plays into what we're going to be doing today. So when we say building a hyperfast web server with Beano, Hyper is a Rust library that is an HTTP implementation. And it's known to be very correct in its implementation, and it's very fast.

Yeah, memory-safe. So I've used Rust and Hyper before. So when this integration came in Beano 1.9, I was super excited about it.

2. Benefits of Rust and Hyper

Short description:

Will discusses the benefits of using Rust and the performance improvements of Hyper over the standard TypeScript implementation of HTTP. They plan to build a Hello World application and a router and server based on Express. They also mention using OAK, a web framework, and offer support for questions and provide setup instructions.

What were you saying, Will? It's memory-safe. It's memory-safe and Rust itself tends to be correct, like it forces you to do things correctly. It doesn't mean that you can't write bugs, obviously, like your logic can be wrong, but it forces you to handle error conditions, if you have enumeration, you have to actually match on all of them and handle every case. So there's lots of things in Rust that force you to do things well.

Well, and the nice thing about this is prior to Hyper, you had a standard TypeScript implementation of HTTP, which it works fine, but it's a little bit slower. Rust is a lower level language than JavaScript, so Hyper has the ability to be a little bit better performance. Right, and we have a much better performance for some things. Yeah, I mean, I'm actually really impressed with how fast the standard HTTP library has been, considering it's completely written in TypeScript. So we're gonna write a little Hello World application with that just to see what that looks like, and we'll use that to do some load testing so we can see the difference whenever we use Hyper.

And then once we go through a Hello World application for a Hyper, we'll build a router and a server around it. We're gonna model this after Express. My assumption is that most people here have used Node and probably have some experience with Express, so we're gonna do that. I mean, typically if I were gonna build a new framework out of the box with Deno, I probably wouldn't model Express. I would do something a little more idiomatic to Deno. Deno uses Web Standards across the board, so most of the things that are in Deno are also available in the browser, which is pretty cool. So you're using Web Standards most of the time. So there's things that I have learned because of Deno that are in Web Standards and available in the browser that I had no idea were there, which has been cool. Like requests and response objects and lower level things that I didn't think were available in the browser are, which is neat.

After that, so we'll create a little framework that does some routing and allows you to add some middleware and then if we have time at the end, we'll use OAK. OAK is more of an idiomatic web server approach written by somebody on the Deno core team. It's available to use, but it's a web framework. So when we're done with this, you're gonna have a little web framework that you've written yourself, which will be really neat to go through that process and see all the things that you gotta think about when you're building a web framework. But I wouldn't recommend it for production. OAK is pretty well supported in the community and I would check that out if you're gonna build your own server, but this will be cool to see some of the internals of how a web framework works.

And just before, or while Matt's getting started here, if you have any questions as we go along, you can post them in chat or there is a Discord channel for this workshop and we'll do our best to answer it as we go along. All right, and I'll try, if we're going too fast or something, just let us know or if you need explanation or more on what we're doing, definitely let us know. So I'm in a weird setup. So if you see me like moving my hand, I don't know if you can even see me, but you see me move my hand. I have my iPad up. That's my second monitor right now. So just have some notes to go through. All right, here we go. So right now I just have a folder. There's nothing in it. So you need to have Deno installed. You need to have at least Deno 1.9 or above. So hyper was added to the Deno API in 1.9. It's still in an unstable state. So we will use the unstable flag for this. I have this other file here. Hey, this is a load tester. I guess I can just pull that up. Yeah, I'm gonna link it in Discord and I'll also link it in chat here. Yeah, this is cool. It's a load tester. It's just a command line utility that you can use to generate load on a URL. And that'll allow us to analyze the performance of each of our solutions that we create. So if you wanna do that with us, definitely download HEI. They're the executables are right here. You just download them and run them. You might have to chmod, then mach1, chmod 777, then the filename. All right, let's build a little server with Deno. Oh, another thing you'll want. I'm in VS Code. There is an extension for Deno, if you search for Deno in your extensions. It's the one that's from Denoland. Denoland is the organization on GitHub for Deno, and they maintain this extension. It used to be third-party, and then they brought it in, which is cool. So get that extension. That'll allow you to do things like top-level awaits, get the typings for Deno, TypeScript and all that. All right, so the first thing that I'm gonna do is I'm actually gonna set this up for Deno. So I'm gonna do Command-Shift-P, and if you type in Deno, you'll get initialize your workspace configuration, and just hit Enter through those, and it'll create a VS code settings file to let the VS code know that this is a Deno project, and the extension will now be in use. All right, so I'm gonna create a folder, and I'm gonna call this one STD, and we're gonna create the hello world implementation in here for the standard library, and you can just create a TS file, and my iPad keeps going blank, so I don't remember the exact name of the module that we're gonna put in. All right. You don't need to clone Hey. Hey, if you want to, I just linked it. We're going to use it, but it's just a load tester. We're not going to write any code or anything like that. Right, yeah. We're just using that so when we write our server, we spin it up, we can point Hey to that and have it generate load and see how many requests per second we're able to get. All right, so the first thing we're gonna do is import serve, and this is the standard library implementation of Hello World, just to give you an idea of what it looks like. Deno uses ESM modules, so we don't have to use NPM install or anything like that. We can literally point this out on the internet, and it'll pull in these files, which is super cool. That's one of the big innovations of Deno as well is to get away from NPM and dependency management and allow you to do it in a more web friendly way. Yeah, Brian, we actually do have a GitHub link for the workshop that has the final version of what we're creating today, so I'll link that later so that people can follow along and not be looking through the final code while we're trying to code through it. We're starting with a blank folder, like you can see there, Matt just has his std slash hello-world.ts and nothing else, so that's what our starting point is today. Yeah, this is super cool. Deno is neat. If you haven't used it before, yeah, you don't have to do anything for a setup. I don't need to create an NPM. I don't have to do any package.json or anything like that. I can just create a TypeScript file and start coding and then we're just gonna run it. There's nothing more to it. The packages can be locally cached, so what you can do, if you're running the VS Code plugin, you can go up and right click on it, I think, and potentially cache the plugin. And that way it's just available for you. Yeah, one thing that's happened to me right now is like it's not finding these things. So sometimes after I initialize my extension, I need to restart VS Code, so I'm gonna do that real quick. So you might need to do that as well. If you're getting red squiggles under the import, then you'll need to do that. So now they went away, so I'm good to go.

QnA