Skip to main content
View on YouTube to comment and like!

Syntax Video

You Should Use Hono in your Next Project

Chapter 1 0:00

Intro

now you might have heard that you should stop using Express or that Express is old and outdated but you're probably curious what should you use instead the answer is hono so hono is a web application framework for building backends with JavaScript and in this video I'm going to show you five reasons why you should try hono and use it in your next project so let's jump in my name is CJ welcome to [Music] syntax and one of the first things I really like about hono is the authoring experience is very similar to express so you can see here I have a basic hono app I can create a git route that responds on the slash path with this specific Json object and we can create any sorts of handlers we have app. poost app. put app. delete Etc and within those handlers of course you can specify a specific route and then we can use our context object here to access the incoming request or do something for the outgoing response and the context object here has a ton of things here to work with both the request and the response

Chapter 2 0:26

Familiar Route Creation

and everything in between so here if I want to respond with an object I can just do cjson give it a banana prop and then now this route should work so if I try it out with thunder client we can see once we make that post request we get back an object with banana true now not only is the authoring experience very similar it also has really good typescript support so let's say I had a route that's like get SL toos SL some ID to get access to this ID in the URL you can do c. request. param and then pass in the specific Pam that you're looking for but you'll notice I get type completion so this actually lets me know what prams are specified in my specific path here and then I can get access to that value I'll just respond with it and if I make a request to it you can see that it responds with that ID so when you're building out your handlers not much new to learn it's very similar to express but we do get really good typescript support for things like prams and some other things that I'll show you in a second now the next thing I really like about hono is that it has first class support for almost every single JavaScript runtime so if you've ever

Chapter 3 1:12

Type Safety

Chapter 4 1:50

Supports Every JS Runtime

tried to get express running in like serverless environments or other places that aren't node.js it can be tricky a lot of times and a lot of times there's like a compatibility layer with hono they have first class support for everything so you can deploy hono to Cloud FL workers cloudfl Pages Dino bun forcell netlify AWS Lambda Etc you can see all of the supported adapters that they have here and whenever you are generating a hono app you can choose where you want to deploy it to and they'll automatically set up that adapter for you so it's super simple to pick your runtime and generate an app that's ready to deploy to that runtime now the next thing I really like about hono is they have thought of everything so as I've building out some apis every time I need to do something it's pretty much built in so for instance they have all of these helpers like getting the connection info of the incoming requests you have a cookie helper for setting and getting and signing cookies all just builtin they also have a built-in JWT package which comes with decode sign and verify so you don't have to install anything extra they also support streaming of requests and websockets and there's a ton of other stuff to you can see there's a bunch of built-in middleware functions so if you need

Chapter 5 2:26

Many Built-in Helpers / Middlewares

basic off you can literally just add their basic off middleware if you need cores you don't have to install anything extra they have a built-in cores module so basically everything that I've wanted to do I can pretty much find already built into hono for example logging so right now our API doesn't actually do any logging of the incoming requests but if I bring in hono logger just pull it in at the top here and then just say app.use logger and now this will automatically log all incoming requests so if we take a look at the terminal here and then I make some requests to my API you can see it logs the method the specific path the incoming request the outgoing response and how long each of those took um and that's just built in you don't have to install anything extra you can just pull it in from their middle Wares here they even have built-in support for jsx so let's say you want to have a route that actually just responds with some HTML instead of having to work with like template strings or setting up a view engine you can literally just have jsx elements and then do contact. HTML and it will actually respond with the server

Chapter 6 3:40

JSX Support

rendered version of that jsx element so they have absolutely everything I would say when you're getting started with hono if there's something specific that you want to do you should search their docs to see if there's already something built in and if you can't find something in their docs just check their get repo too there's a packages folder here under middleware and for instance you could pull in the Sentry middleware so this video is brought to you by Sentry and if you want to add error logging to your hono app it's super simple literally just install the senty middleware here and then use it in your app pass in your DSN and now you're actually logging errors in production so in all the testing I've been doing I have not come across a single thing that I wasn't able to find in the docs or in the GitHub repo uh so they really have thought of everything next thing I really like about hono is built-in support for validation so you can see here in the docs in the validation section you can specify a validator which is a type of middleware you specify the type of validator that it is and this will validate every incoming request so you can see in this example they're validating the incoming form data but you can also validate the prams in the

Chapter 7 4:11

hono sentry middleware

Chapter 8 4:35

Built-in Validation

URL like for instance make sure that this ID is actually a number you can valid validate all of the query parameters and you can also validate the incoming Json body so this is great because you don't have to do all of your validation inside of your request handlers you can essentially set up all of your validators and then inside your Handler you can do c. rec. valid and then pass in the specific thing that you want access to like the form or the query the pams or Json and it will only give you back the item that has been validated and if it was invalid you can actually handle that here in this hook so you can have like a standardized error Handler that you pass into all of your validators so this is awesome I pretty much do this in every Express app but I'm like setting it all up manually um but what's even better is that they have a Zod validator middleware so you can specifically pass in a Zod schema and it's automatically going to call that parse function for you but it doesn't just stop there because there is a community package called Zod open API and this allows you to not only validate incoming requests with Zod but you can

Chapter 9 5:47

OpenAPI with Zod

also set up open API documentation so you can see here we Define a couple of schemas and then we have this create route method where we specify the method the path and then describe the incoming request and the outgoing responses and then it exposes this open API method here where you pass in the route definition and then you have a fully typed Handler here so because this route definition here comes before our Handler it's actually going to have built-in typescript support to make sure that your response is in line with the schema that you pass in here and you can get access to the incoming requests like the prams or the body or whatever else and this will be fully valid validated inside of your Handler here so actually buil out a full project using Zod open API and it is fantastic and of course because it's open API they have built-in support for Swagger so all of this UI is automatically generated based on the routes that I've defined so I have a login route which accepts an email and a password I've got a users route that will respond with all users in the system you can see that it describes the specific responses here and then I have

Chapter 10 6:31

OpenAPI Full Example

a route that allows me to create a user and you can see I've documented uh a successful response and invalid response where I'm actually returning the Zod validation error and in the case of getting a user by ID there's a potential validation error of someone not passing in a number as the ID so what's great is all of this is fully integrated and really all I had to do was Define my routes and then it automatically generates this documentation so let me show you how I set that up so here in my project I have a routes folder called it users the entry point just pulls in all of the route definitions and their individual handlers so I really wanted to put hono to the test to see can I build a bigger application can I start to separate things out in a way that I'm used to like in Express and I absolutely was you can see here I'm passing in the route definition and the handlers and then if we look at the specific route definitions you can see that for get all users this is a git method on/ users it'll respond specifically with the user attributes whenever making a select from the database and then for git user we

Define the method the path and this case we want to validate that incoming ID to make sure it's a number so I actually have a Zod validator to make sure that that is a number and then for a successful response it's going to respond with a user if there was an error it is a validation error and then also you might run into a not found error if you pass in an ID and that user is not in the database so I also have a schema that defines the not found error and then same thing for create user it's a post request on/ users the incoming body is going to be validated to match my insert user schema and then of course I Define all of my responses so you could think of this file as kind of setting up all of the definitions of everything in terms of what routes should exist what do they validate what do they respond with and then from there we can have the actual implementation so if you look in my handlers file so inside of here I have my actual handlers you can see that we have the context we can use our data access layer to actually query the database and then respond accordingly and then each of these is fully typed so I have a little type helper here you can pass in the

route definition so it'll actually give you a typ error if you try to respond with something that doesn't match the specific responses that you said this route could potentially respond with so that's awesome as well now for the get user Handler you can see I'm getting access to that validated ID which is a number and then in the case of creating a user you can see I get access to the validated user to actually insert into my database now the last thing that really brings all of this together is that all of these schemas are actually inferred from a drizzle table so ultimately my source of Truth is my drizzle schema so I Define my user table here with Drizzle and this is really the only place I need to touch if I ever need to add any new columns or change column types or anything else like that and then from there I'm using the uh drizzle Zod to automatically turn this table schema into a Zod validator and that's actually what I'm passing into all of those route definitions so if you look at my schemas file I just pass in the table schema that I defined to this create insert schema and create select schema from drizzle Zod and that automatically gives me a schema that I

can use inside of my open API uh definition which also gets used as a validator for incoming requests so uh the these methods are built into drizzle and they're great because you can take an existing table definition and just turn it into a Zod validator but you can also modify it so in the case of inserting I also want to make sure that that email is a valid email so I can actually use the Zod email validator and then whenever I'm inserting into the database I don't want to include the ID or the created at field because those are automatically generated so I can use zod's omit to say don't include those properties in this specific insert validator and then lastly I can give it a name and so when we look at the actual open API documentation you can see all of the SCH schemas are defined down here and we can actually see what their types are but essentially the name of that schema comes from passing it in here so this is a beautiful way to build apis especially if you're using something like drizzle where you can have a single place where the table definition is defined and then everything flows from there we can create our validator automatically we can pass those

validators into our route definitions it is a beautiful thing now if you'd like to see a full tutorial of how I set all of this up with Drizzle and everything else let me know down in the comments because I'd love to dive a bit deeper into how I set all of this up but wait there is more so not only can I have a fully generated Swagger UI just based on the code that I've defined hono also has a built-in RPC client so you can actually take the type of a defined hono app pass it into hono client and then you get a fully typed client that makes API requests to the endpoint that you've set it up with and we get the same thing with the open API router that I defined so you can see here I'm importing that exported type that is literally just the open API router that I Define that has all of the specific routes on it but then we get a fully typed client so if I'm doing users. poost I get a typescript error if I'm not passing in all of the right properties and then I can automatically parse the response as Json and then all of the other routes are available as well you can see that I get type completion for the specific router and from there we can see what methods are available to me including

Chapter 11 11:00

Hono RPC Client

the ID route so if I want to call get users by a specific ID I can specify that and then say get and then we here we'll get validation on the Pam so it knows that I need to pass in a Pam for the ID and it won't be happy until I've actually fixed all of the typescript errors here so this is also fantastic literally I have a single source of truth I Define my drizzle table I then Define all of my routes based on that schema and then even the client schema can be inferred from all of that so just working with this is is kind of amazing it's like the gold standard of really having good type safety everywhere and having a single source of truth in your codebase now one of the last reasons to try hono is for hono X so hono X is a meta framework that's built on top of hono that gives you serers side rendering file based routing uh islands all the things that you would want from a meta framework um it's currently an alpha but it looks really promising so you can use everything you know about hono but on top of that you can now render routes using jsx and then also have client side components also authored with react and their built-in

Chapter 12 12:23

HonoX Meta Framework

jsx helper so this is definitely something to look out for And So eventually once it gets out of alpha it'll be a really good alternative instead of having to use something like nextjs so that's it for the main things that I like about hono I would say if you're even slightly curious just give it a try go to their getting started run the create command choose your specific deployment platform and then start building things out and like I said pretty much everything I've needed is here in middleware or helpers they also have this examples section so these are more specific examples to the types of things that you might want to do so you can check out that example as well like I said if you want to see me do a full tutorial on this or dive in deeper leave a a comment down below and that's all I have for now so I'll see you in the next one [Music]

Chapter 13 12:56

Thanks!

Quote it with credit and a link. Quoting Syntax