Build Your Web Framework From Scratch!

This ad is not shown to multipass and full ticket holders
JSNation US
JSNation US 2025
November 17 - 20, 2025
New York, US & Online
See JS stars in the US biggest planetarium
Learn More
In partnership with Focus Reactive
Upcoming event
JSNation US 2025
JSNation US 2025
November 17 - 20, 2025. New York, US & Online
Learn more
Bookmark
Rate this content

Like Hono, you can easily create a backend web framework using Web Standards APIs such as Request and Response. These run on Cloudflare Workers, Deno, Bun, etc. In this talk, I will introduce how to make it, such as implementing routing using URLPatterns, Hono/Express-like syntax, middleware mechanisms, and server-side rendering with React. This is reinventing the wheel, but making your framework is fun!

This talk has been presented at JSNation 2025, check out the latest edition of this JavaScript Conference.

FAQ

Yusuke's talk at JS Nation was titled 'Build Your Web Framework from Scratch'.

Yusuke is a developer advocate at Cloudflare and the creator of the web framework Hono.

Middleware functions execute before and after the main handler in the web framework, allowing modifications to requests and responses, and reuse across routes.

A 'handler' is a function that processes incoming requests and returns a response. It can be customized with headers and status codes.

Developers can test their applications by creating requests and passing them to the app's fetch method, checking the response object's status and body without needing to run a server on localhost.

The 'context' is an object that includes request data, match results, and can be extended to hold custom values, facilitating communication between middleware and handlers.

According to Yusuke, a web framework simplifies the development process by handling routing, parameters, different response types, authentication, cross-origin requests, and security features, making code more readable and maintainable.

The web framework handles routing by defining routes with methods, paths, and handlers. It uses URL patterns to match requests and execute the appropriate handler.

The web framework discussed in Yusuke's talk is built using JavaScript and TypeScript.

Helpers are reusable functions that provide useful logic, such as rendering HTML. They receive context and can be accessed using 'C.helper' inside handlers.

Yusuke Wada
Yusuke Wada
18 min
16 Jun, 2025

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Excited to talk at JS Nation about building web frameworks from scratch, covering framework basics and providing an example without a framework. Framework essentials include handling browser views, routing, parameters, response types, errors, and security. Discussing the components a framework needs such as routing, parameters, response types, error handling, authentication, and security. Exploring request handling in a framework with customization of headers, status, and using static methods like response.json. Detailed insights into routing, response customization, and efficient implementation for multiple responses based on paths and methods. Implementation of routing syntax, data retrieval from requests, context handling, middleware integration, and helper functions for response manipulation in a framework.

1. Building Web Framework Intro

Short description:

Excited to talk at JS Nation. Build Your Web Framework from Scratch. Agenda: Creating web frameworks, framework basics, example without framework.

Hi. I'm very excited to talk at JS Nation today. My talk title is, Build Your Web Framework from Scratch. My name is Yusuke. I love building web frameworks. I work at Cloudflare as a developer advocate. I'm a creator of Hono.

This is today's agenda. Creating web frameworks is fun. Let's create a web framework. Before I talk about what is a web framework, here are a few things you should know. This framework is built with JavaScript and TypeScript. It works on Cloudflow.

Let's look at an example without a framework. This is the smallest web application you can write. You just return a response from fetch function. You can learn the same code on different runtimes. For example, Cloudflare has Deno or BAN. Just run one of these commands.

2. Framework Essentials

Short description:

Browser view using Angular. Application complexity: need for routing, parameters, response types. Framework handles actions based on URL, extracts values, manages responses, errors, security.

This is what you see in your browser when you're using Angular. Just go to localhost 8787 and it shows hello. It works the same on Deno or BAN, too.

But in the application, things get complex very quickly. For example, you need routing like get slash or post slash post, and you have to handle all of that yourself. Writing everything manually becomes long and hard to read due to too many conditions. Instead, think about what we need in a framework: routing, parameters, different response types, and more.

The framework needs to decide actions based on URL path and method, extract values from the URL, and handle different response types. Additionally, it must manage notepads, errors, future rights, authentication, cross-origin, and security. Using a framework like Hono results in shorter, more readable, and maintainable code. It enables quick application development, scalability, and the ability to understand the framework's internal workings by building one yourself.

3. Framework Components

Short description:

What a framework needs: Routing, parameters, response types, handling notepads, errors, future rights, authentication, cross-origin, security. Using a framework like Hono for shorter, readable, and maintainable code. Building your own framework can be fun. Creating a basic structure for a framework with a fetch method to return responses.

Instead, let's think, what do we need in a framework? Routing, parameter, different response types and so on. Let me explain them one by one. First, the framework needs to decide what to do based on URL path and method. For example, get slash, post slash post. The framework needs to extract the values from the URL. Like parameters in the path. The framework might return text, JSON, HTML, or even do a re-write depending on the situation. The framework also needs to handle notepads and errors. And in real-world application, future rights, authentication, cross-origin, and security things are essential. This is the same example as before. But written in Hono. It's much shorter and readable and easy to maintain. This is the benefit of using a framework. So, this is the framework. It's short, easy, and very readable. With it, you can build an application really quickly and it even scales well when your application gets bigger. You can use Hono, but let's build one yourself. That way, you can see how it works inside and you can see building your own framework is really fun.

Next, let's talk about returning response. Here's our first step. Let's make a function called create app. It's just an object with a fetch method. And this method returns a response. This is the simplest structure for a framework. We call create app. And it returns an object. This object has this fetch method. So, when the request comes in, it will go to fetch and we get the response. So, now, do we return a response? It's simple. We just return the response.

4. Request Handling in Framework

Short description:

Customizing headers, status. Using static methods like response.json, response.redirect. Basic framework building: request to response via a handler function. Defining handlers for requests. Handling cases without defined handlers or errors.

And you can customize it. Set headers, set status. And you can also use static methods like response.json or response.redirect.

When we build a framework, the most basic thing is to take a request and return a response. To do this, we need a function. We call it a handler. The user can define a handler using this setter. The handler is the word internally and the fetch code.

If there is no handler, the framework returns not one. So, here's how it works in the code. This is this defined handler. And this is the setter of the handler. It stores the handler function inside.

5. Routing and Response Customization

Short description:

Handling requests with defined or default handlers. Setting status, content type, and customizing responses. Introduction to routing and multiple route responses based on paths and methods. Utilizing URL patterns with wildcards and path parameters for flexible routing.

When a request comes in, it calls the handler. If no handler is set, it returns nothing. Defining a handler using app.handler results in a new response. By default, the status is set to 200 and the content type to text/plain. To return HTML, set the content type to text/HTML. Customizing the status is possible by using methods like response.json, which returns a JSON response with the correct headers, or response.redirect for a redirect response.

Returning a response involves writing a handler that allows customization of status and headers. Additional helper methods can simplify this process. Moving on to routing, the framework currently handles a single route. In real-world applications, responses vary based on the path and method. For instance, GET / should return the main page, GET /welcome should display the welcome page, and POST /post should create a new post.

URL patterns are instrumental in this process, supporting wildcards and path parameters. The syntax is similar to path-to-regex used in Express, and patterns can be created using new URLs with specific path names. For example, setting the path name as /post/:ID allows for testing with /post, resulting in the first response.

6. Efficient Routing Implementation

Short description:

Customizing status and headers, simplifying with helper methods. Routing for multiple responses based on paths and methods. Utilizing URL patterns with wildcards and path parameters for flexible routing. Defining routes and handlers, updating app functions for efficient routing handling.

We can customize things like status and headers and use helper methods to simplify the process. Moving on to routing, the framework currently handles only one route, but in real-world applications, different responses are needed based on paths and methods. For instance, GET / should return the homepage, GET /welcome the welcome page, and POST /post to create a new post. URL patterns with wildcards and path parameters, similar to Express's path-to-regex, offer flexibility in routing.

API works on Cloudflare, Cardino, and Node.js, allowing the use of wildcards and path parameters. Creating patterns using new URLs with specific path names, such as /post/:ID, enables testing and matching routes. The framework is ready to incorporate routing with URL patterns, defining route types with HTTP methods, URL patterns, and handler functions. Updating the create app function to include methods like on (for storing routes based on URL pattern) and fetch (to match and call handlers) completes the routing feature implementation.

This process allows for handling routing efficiently. The on method stores routes based on methods, paths, and handlers, while the fetch method matches incoming requests with defined routes. If a match is found, the corresponding handler is called, ensuring proper routing. Utilizing these features facilitates the creation of a functional web framework with the ability to handle multiple routes and responses based on specific paths and methods effectively.

7. Framework Routing Implementation

Short description:

Syntax based on path to regex. Creating pattern with new URL. Defining route type, updating create app function. Using on and fetch methods for routing implementation. Testing application by creating requests and checking responses.

It's syntax based on path to regex that is used in Express. We can create a pattern using a new URL with a path name. This sets the path name as slash post slash colon ID. When we test with slash, it returns first. But slash post slash one to three matches. So, it returns true.

Now we are ready to add routing with URL patterns. Let's make a framework with routing. We define the route type that includes the HTTP method and URL pattern and the handler function. Now, we update the create app function. It returns an app object that has two methods, on and fetch. We define the on method. It takes the method, path, handler, and stores them as a route using the URL pattern.

The fetch method goes through all routes, checks if the method and URL match the route, and calls the handler if there's a match. If not a match, it returns 'not found.' This is how you use the routing feature we just built. You define the method, path, and handler using the on method. We built response handling and routing. Now, let's make sure they work as expected. Let's write some tests. To test our application, we can create a request and pass it to app.fetch. The application returns a response object, so we can check its contents like status or body.

8. Data Retrieval from Requests

Short description:

Writing tests for the application using app.fetch. Testing status and body of the response. Implementing routing in the framework. Retrieving data from requests based on URL parameters and query strings.

Let's write some tests. To test our application, we can create request and pass it to app.fetch. The application returns a response object. So, we can check its contents, like status or body. Here's a basic test. We register the routes, then create a request, and pass it to app.fetch. We get a response. We can check the status and body.

What's interesting is you don't need to run the server on localhost; it's just a regular function call. For example, you can test it. By the way, Hono has over 20,000 lines of test code, and most of them follow the same pattern. We now have routing in our framework. We define routes using on, match them with the URL pattern, and handle them in fetch, and we can test it.

Next, let's get data from the request. When we handle a request, we often need to get information from the URL, like path or query strings. We can use the request object to get the URL. Just pass the URL property into the URL constructor to access it. We can access the path name property. We use URL pattern.exec to get the match result. Then, we retrieve the path parameter ID, 123, from match.passname groups. We use URL search params to get query parameters, like page 4, 5, 6.

9. Context Handling and Middleware Integration

Short description:

Getting pass and query parameters from requests. Implementing context to encapsulate request and match info. Enhancing framework with middleware for request and response manipulation.

Then we get the pass parameter ID, 123, from match.passname groups. We use URL search params to get query params, like page 4, 5, 6. We want to pass things like request and match info to the handler. Instead of passing them separately, we wrap them into a single object. This object is called context, and it's easy to extend later. Here's how we define context type. It includes request and match result. Then we use that as the input for the handler function.

In fetch, we find the route that matches URL and method. We create context and pass it to the handler. Here's how we use the context in DR handler. Inside of the context, we have match result from this is URL pattern result, using that, we can get the pass parameter and query parameter. I'll summarize this section. We can get data from requests by running URL.exec. From this match result, we can get both pass and query parameters. We then pass them to the handler inside the context.

Now let's make our framework more powerful by adding middleware. Middleware learns before and after the handler. It can change the request or the response, and you can reuse it across routes. Here are some examples of middleware. These are from Hono, and they show how powerful and reusable middleware can be. From authentication to validation to caching, middleware can do a lot. Sometimes middleware needs to store some values or change the response before returning it. So, let's extend the context to make that possible. We add two more fields to the context. This lets us modify the response before returning. And this bars is a place to store values across middleware. We pass the both lists into the context. We define lists before the loop so that handler can update. And we return the response after the loop.

10. Helper Functions and Contextual Access

Short description:

Using middleware to modify responses. Introducing helper functions for logic like rendering HTML. Enhancing framework with helper objects for contextual access and response manipulation.

Here's how we use lists and bar in practice. The first middleware starts values in bars. The main handler creates the response using stored value. This, the last middleware, modifies the response before returning it. Middleware is a function that learns before and after the handler. It can be used to modify requests, the response, or add useful values. And we designed the context object to hold the key data that the handler needs, like requests, the response object, and customized values.

Let's add the helper feature to our framework. A helper is a reusable function that provides useful logic, like rendering HTML. It receives context so it can access things like requests or response. These are examples from Hono. You can see it has many built-in helpers, like cookies, Jot, HTML, and more. Let's define helpers in our framework. We extend the context type with helper object. The application will have maps of helpers. Each helper is a function that gets context and helper's parameters.

Then in fetch, we pass the helper to the context. Now, instantly in Handler, you can access it as C.helper. Here's how to define a helper called HTML. And this is how to use in Handler. You can call C.helper or HTML to return a new response with HTML contents. If you set the types property, TypeScript will give you autocomplete for your helpers. It's a small feature, but it improves DX a lot. Let's summarize the helper feature. A helper is a reusable function that receives context. You define them in our create app and access them using C.helper. Inside your Handler, if you define the types correctly, you get autocomplete and types safely. Let's look at some practical examples using our framework. Here, we get the path parameter ID and the query parameter page. Then we return JSON response using response.json.

11. Helper Logic Expansion and Framework Completion

Short description:

Turning parameter logic into reusable helpers for cleaner code. Enabling server-side rendering with React using helpers. Completing web framework basics, encouraging further enhancements and project integration.

Here, we turn the parameter logic into helpers. Inside the results, we just call C.helper params or C.helper search params. This makes the Handler cleaner and the helper logic reusable. This example combines multiple features. We define HTML helper and set values in middleware and then use both in Handler. After that, another middleware modifies response helpers. This shows how helpers, contexts, and middleware work together.

This is very interesting. This helper lets do server-side rendering with React. It uses render to read a stream and return HTML response. Now, you can use JSX directly in your Handler. We've now completed all the steps. So, you've built the basics, but there are more you could do. You can improve middleware and helpers or even integrate with real projects like Hono.

Let's wrap up. You built your web framework. The application with your framework can return responses, define routes, get data from requests, and even use middleware and helpers. You just reinvented the wheel, but building your web framework is actually really fun. That's all. Thank you so much.

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

Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
Full Stack Documentation
JSNation 2022JSNation 2022
28 min
Full Stack Documentation
Top Content
The Talk discusses the shift to full-stack frameworks and the challenges of full-stack documentation. It highlights the power of interactive tutorials and the importance of user testing in software development. The Talk also introduces learn.svelte.dev, a platform for learning full-stack tools, and discusses the roadmap for SvelteKit and its documentation.
SolidJS: Why All the Suspense?
JSNation 2023JSNation 2023
28 min
SolidJS: Why All the Suspense?
Top Content
Suspense is a mechanism for orchestrating asynchronous state changes in JavaScript frameworks. It ensures async consistency in UIs and helps avoid trust erosion and inconsistencies. Suspense boundaries are used to hoist data fetching and create consistency zones based on the user interface. They can handle loading states of multiple resources and control state loading in applications. Suspense can be used for transitions, providing a smoother user experience and allowing prioritization of important content.
From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Tanstack Start - A Client-Side First Full-Stack React Framework
React Summit US 2024React Summit US 2024
30 min
Tanstack Start - A Client-Side First Full-Stack React Framework
Top Content
We surveyed thousands of developers to show that a louder audience leads to a better presentation. There has been a shift in web app development towards server-first architectures, which has improved full-stack capabilities but at the cost of complexity and divergence from the client-centric approach. Tanstec Start is a meta-framework that aims to provide the best client-side authoring experience with powerful server-side primitives. The Tansec Router supports advanced routing features, URL state management, and JSON storage. Combined with the server-side rendering capabilities of TanStack Start, it becomes even more powerful. The TanStack Router has isomorphic loaders and integrates seamlessly with TanStack Query for additional features like polling and offline support. UseSuspenseQuery allows for dynamic streaming of data during SSR. TanStack Start also offers server-side features, API routes, server functions, and middleware. The future plans include RSCs, websockets, real-time primitives, and static pre-rendering. TanStack Start is now in beta and is suitable for building React apps. It is open source.

Workshops on related topic

Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Back to the Roots With Remix
React Summit 2023React Summit 2023
106 min
Back to the Roots With Remix
Workshop
Alex Korzhikov
Pavlik Kiselev
2 authors
The modern web would be different without rich client-side applications supported by powerful frameworks: React, Angular, Vue, Lit, and many others. These frameworks rely on client-side JavaScript, which is their core. However, there are other approaches to rendering. One of them (quite old, by the way) is server-side rendering entirely without JavaScript. Let's find out if this is a good idea and how Remix can help us with it?
Prerequisites- Good understanding of JavaScript or TypeScript- It would help to have experience with React, Redux, Node.js and writing FrontEnd and BackEnd applications- Preinstall Node.js, npm- We prefer to use VSCode, but also cloud IDEs such as codesandbox (other IDEs are also ok)
Let AI Be Your Docs
JSNation 2024JSNation 2024
69 min
Let AI Be Your Docs
Workshop
Jesse Hall
Jesse Hall
Join our dynamic workshop to craft an AI-powered documentation portal. Learn to integrate OpenAI's ChatGPT with Next.js 14, Tailwind CSS, and cutting-edge tech to deliver instant code solutions and summaries. This hands-on session will equip you with the knowledge to revolutionize how users interact with documentation, turning tedious searches into efficient, intelligent discovery.
Key Takeaways:
- Practical experience in creating an AI-driven documentation site.- Understanding the integration of AI into user experiences.- Hands-on skills with the latest web development technologies.- Strategies for deploying and maintaining intelligent documentation resources.
Table of contents:- Introduction to AI in Documentation- Setting Up the Environment- Building the Documentation Structure- Integrating ChatGPT for Interactive Docs
Learn Fastify One Plugin at a Time
Node Congress 2021Node Congress 2021
128 min
Learn Fastify One Plugin at a Time
Workshop
Matteo Collina
Matteo Collina
Fastify is an HTTP framework for Node.js that focuses on providing a good developer experience without compromising on performance metrics. What makes Fastify special are not its technical details, but its community which is wide open for contributions of any kind. Part of the secret sauce is Fastify plugin architecture that enabled developers to write more than a hundred plugins.This hands-on workshop is structured around a series of exercises that covers from basics "hello world", to how to structure a project, perform database access and authentication.

https://github.com/nearform/the-fastify-workshop
Build a Product Page with Shopify’s Hydrogen Framework
React Advanced 2022React Advanced 2022
81 min
Build a Product Page with Shopify’s Hydrogen Framework
Workshop
David Witt
David Witt
Get hands on with Hydrogen, a React-based framework for building headless storefronts. Hydrogen is built for Shopify commerce with all the features you need for a production-ready storefront. It provides a quick start, build-fast environment so you can focus on the fun stuff - building unique commerce experiences. In this workshop we’ll scaffold a new storefront and rapidly build a product page. We’ll cover how to get started, file-based routing, fetching data from the Storefront API, Hydrogen’s built-in components and how to apply styling with Tailwind.You will know:- Get started with the hello-world template on StackBlitz- File-based routing to create a /products/example route- Dynamic routing /products/:handle- Hit the Storefront API with GraphQL- Move the query into the Hydrogen app- Update the query to fetch a product by handle- Display title, price, image & description.- Tailwind styling- Variant picker and buy now button- Bonus if there’s time: Collections page
Prerequisites: - A Chromium-based browser (StackBlitz)- Ideally experience with React. A general web development background would be fine.
Build a Universal Reactive Data Library with Starbeam
JSNation 2023JSNation 2023
66 min
Build a Universal Reactive Data Library with Starbeam
WorkshopFree
Yehuda Katz
Yehuda Katz
This session will focus on Starbeam's universal building blocks. We'll use Starbeam to build a data library that works in multiple frameworks.We'll write a library that caches and updates data, and supports relationships, sorting and filtering.Rather than fetching data directly, it will work with asynchronously fetched data, including data fetched after initial render. Data fetched and updated through web sockets will also work well.All of these features will be reactive, of course.Imagine you filter your data by its title, and then you update the title of a record to match the filter: any output relying on the filtered data will update to reflect the updated filter.In 90 minutes, you'll build an awesome reactive data library and learn a powerful new tool for building reactive systems. The best part: the library works in any framework, even though you don't think about (or depend on) any framework when you built it.
Table of contents- Storing a Fetched Record in a Cell- Storing multiple records in a reactive Map- Reactive iteration is normal iteration- Reactive filtering is normal filtering- Fetching more records and updating the Map- Reactive sorting is normal sorting (is this getting a bit repetitive?)- Modelling cache invalidation as data- Bonus: reactive relationships