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

Watch more workshops on topic

Node.js Masterclass
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Top Content
Workshop
Matteo Collina
Matteo Collina
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
GraphQL - From Zero to Hero in 3 hours
React Summit 2022React Summit 2022
164 min
GraphQL - From Zero to Hero in 3 hours
Workshop
Pawel Sawicki
Pawel Sawicki
How to build a fullstack GraphQL application (Postgres + NestJs + React) in the shortest time possible.
All beginnings are hard. Even harder than choosing the technology is often developing a suitable architecture. Especially when it comes to GraphQL.
In this workshop, you will get a variety of best practices that you would normally have to work through over a number of projects - all in just three hours.
If you've always wanted to participate in a hackathon to get something up and running in the shortest amount of time - then take an active part in this workshop, and participate in the thought processes of the trainer.
Mastering Node.js Test Runner
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Marco Ippolito
Marco Ippolito
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
ESM Loaders enhance module loading in Node.js by resolving URLs and reading files from the disk. Module loaders can override modules and change how they are found. Enhancing the loading phase involves loading directly from HTTP and loading TypeScript code without building it. The loader in the module URL handles URL resolution and uses fetch to fetch the source code. Loaders can be chained together to load from different sources, transform source code, and resolve URLs differently. The future of module loading enhancements is promising and simple to use.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
This talk covers various techniques for getting diagnostics information out of Node.js, including debugging with environment variables, handling warnings and deprecations, tracing uncaught exceptions and process exit, using the v8 inspector and dev tools, and generating diagnostic reports. The speaker also mentions areas for improvement in Node.js diagnostics and provides resources for learning and contributing. Additionally, the responsibilities of the Technical Steering Committee in the TS community are discussed.
Node.js Compatibility in Deno
Node Congress 2022Node Congress 2022
34 min
Node.js Compatibility in Deno
Deno aims to provide Node.js compatibility to make migration smoother and easier. While Deno can run apps and libraries offered for Node.js, not all are supported yet. There are trade-offs to consider, such as incompatible APIs and a less ideal developer experience. Deno is working on improving compatibility and the transition process. Efforts include porting Node.js modules, exploring a superset approach, and transparent package installation from npm.
Javascript Should Come With Batteries
React Day Berlin 2023React Day Berlin 2023
30 min
Javascript Should Come With Batteries
Watch video: Javascript Should Come With Batteries
JavaScript Should Come With Batteries: Deno is a next-generation JavaScript runtime that addresses the lack of built-in tooling in JavaScript. It provides a secure and simple way to develop applications with built-in testing, linting, formatting, and a language server for VS Code. Deno is compatible with Node.js and NPM, supports web standard APIs, and allows code portability between frontend and server. It also offers features like a built-in database, a key-value store, and transparent monetization with Deno Deploy.