A Better Way to Write APIs?

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025
  • Turns out a method i found out that seemed pretty hacky to me at first because it messes with framework defaults actually works quite well. It's pretty fast, cheap, and easy to set up. Honestly there might be bugs that I haven't found yet, so please dont use this in production yet lol
    -- sources for this video
    hono: hono.dev/
    cloudflare workers: workers.cloudf...
    -- my links
    saas: www.animate-co...
    newletter: www.joshtriedc...
    discord: / discord
    github: github.com/jos...

ความคิดเห็น • 150

  • @KrishnaJha25
    @KrishnaJha25 11 หลายเดือนก่อน +5

    I’m working on a project with a client using CF worker, CF worker for platform, hono and solid js and trust me things are super fast. We are talking about single digit millisecond performance. So yeah this works

  • @MaximeDehaye
    @MaximeDehaye 11 หลายเดือนก่อน +10

    From where the difference in speed is coming from then ? If both are hosted on CF anyway

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน +5

      honestly no idea, I'd like to know as well

    • @sanampakuwal
      @sanampakuwal 11 หลายเดือนก่อน +6

      I guess that might be vercel overhead like: logging, analytics, routing, reading config, etc. But I'm not totally sure.

    • @MagerX1794
      @MagerX1794 11 หลายเดือนก่อน +1

      @@joshtriedcoding vercel has a ton of logging and middleware and other built in utilities that it needs to step through is my presumption.

    • @elias-soykat
      @elias-soykat 7 หลายเดือนก่อน +1

      Vercel api route use as serverless function and it's not fast in comparison to cloudflare workers

    • @Hacking-Kitten
      @Hacking-Kitten หลายเดือนก่อน

      Aws Lambda vs CF worker.. V8 vs Node

  • @vigneshm5044
    @vigneshm5044 11 หลายเดือนก่อน +9

    Nice insight.
    For real world apps with database, the latency can be worse twice as much.
    If the database is located at the same location of cloudflare, fetch from Database will be fast, but that won't be the case since cloudflare workers are global.
    So once the request reaches cloudflare, another request has to go to the database. And if the request issues multiple DB queries, there will multiple requests to DB. Eg find user and then find orders.
    So in case of requests requiring data from DB, better to colocate the server and DB in same region

    • @GameBully2K
      @GameBully2K 7 หลายเดือนก่อน +1

      Cloudflare launched D1 and KV which are respectively a Dicentralized SqlLite Db and an On Memory Key Value Database. And theoraticly when you hit a Cloudflare worker it should communicate with the closest replicat of the dbs.

  • @st-jn2gk
    @st-jn2gk 11 หลายเดือนก่อน +24

    Vercel has something called “regional edge” where they deploy your backend to a cloud flare worker localized to a region like east US or whatever. Normal cloudflare does not do that. I don’t know if your vercel deployment was using regional edge, but if it was, that would explain the speed difference. Regional edge allows you to make multiple DB queries without the downside of the distance cost from a distributed edge to a centralized db. I think you can test it by having the backend do like 3 queries to a database. If Vercel is still slow in that test, then this is an easy cloud flare W. Cool video king, I love high effort stuff like this.

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน +4

      interesting, I wonder if the regional edge thing could be the reason for the difference or if its also the vercel tracking, logging etc. Cheers man!

    • @st-jn2gk
      @st-jn2gk 11 หลายเดือนก่อน

      true. it might be the deployment region or the proprietary layers of logging and tracking. Cheers back!@@joshtriedcoding

    • @avid459
      @avid459 11 หลายเดือนก่อน

      I am waiting for their latest offering, the ultra pro edge, where the code to be executed will be even closer than the edge, it will be on the users device.😎

    • @adamconrad5249
      @adamconrad5249 11 หลายเดือนก่อน

      standard AWS feature

    • @st-jn2gk
      @st-jn2gk 11 หลายเดือนก่อน

      ? not its not wdym@@adamconrad5249

  • @jitxhere
    @jitxhere 11 หลายเดือนก่อน +51

    I really like Hono + Clouflare Workers. Also D1 works fine too. I sometimes use Turso or D1

    • @yourlinuxguy
      @yourlinuxguy 11 หลายเดือนก่อน +1

      Can you tell how to access env vars in hono? Outside ant .get methods?

    • @deo_kumar
      @deo_kumar 10 หลายเดือนก่อน

      ​​@@yourlinuxguyyou can't access env variables in top level of your module. If you want to use any library that requires env variables. You should use hono's vars middleware if you are using hono.
      Like this:
      const app = new Hono();
      app.use(async (c, next) => {
      c.set("myDb", new DB(c.env.MY_ENV));
      await next();
      });
      app.get("/", (c) => {
      const db = c.get("myDb");
      return c.json({...});
      });

    • @deadpeddler
      @deadpeddler 9 หลายเดือนก่อน

      ​@@yourlinuxguyif you find out ,let me know

    • @yourlinuxguy
      @yourlinuxguy 9 หลายเดือนก่อน

      @@deadpeddler you can't access .env outside the .get methods

    • @jhoan-me
      @jhoan-me 4 หลายเดือนก่อน

      @@yourlinuxguy you can't

  • @diegoquiroz1059
    @diegoquiroz1059 11 หลายเดือนก่อน +2

    Great! But the problem with edge or regional runtimes like CF Workers is when the distance between the worker (backend) and its other sources matters.
    If the regional backend needs to fetch multiple times from a far away source (e.g. database on US-East), it will be worst. If it's one call, it will be the same time overall.
    client backend database [backend is near the client]
    client backend database [backend is near database]
    This shines when the database read replica is regional too, like Turso

  • @ajaymandal2560
    @ajaymandal2560 11 หลายเดือนก่อน +2

    This is amazing guide, watch step by step while integrating hono in my nextjs app. Thanks alot

  • @mendylanda125
    @mendylanda125 11 หลายเดือนก่อน +38

    Saying it’s double the performance is a bit of a stretch, in a basic example such as yours you’re saving the 10ms overhead that Vercel requires (understandably- Logging, Billing, Auth, etc etc).
    The real question is how does it compare in a real world use case, my bet is it will remain in the 10ms range, and definitely not “double”.
    Great vid though ❤

    • @aryan.prince
      @aryan.prince 11 หลายเดือนก่อน +1

      yeah just like you mention, i also wonder if hono/CF workers will actually take half as long, or just within ±10ms compared to Vercel Edge network
      i haven't tested this on a large scale with more complex API calls, curious what y'all think about this

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน +13

      in my tests (production environment) I got about a ~30% consistent speed difference for db interactions in both API routes. Decided to leave it out cause that part was just boring, probably wouldve been a good idea to at least include the results. Fair point, appreciate ya

    • @kevboutin
      @kevboutin 17 วันที่ผ่านมา

      These tests are flawed severely as the backend doesn't really do anything. Add persistent storage, workflow or some other decoupled tasks and you will notice your edge gains are now trashed because you just added a LOT of latency to your other backend services that do NOT exist on the edge.

  • @FabuBrik
    @FabuBrik 11 หลายเดือนก่อน +116

    FAST_MODE: true is hilarious.

  • @Goshified
    @Goshified 11 หลายเดือนก่อน +1

    "yeet" as a dummy commit message. I feel so seen right now.

  • @randomname8217
    @randomname8217 10 หลายเดือนก่อน

    So the only difference is switching the backend host? Wow, that's really a valuable insight

    • @Argosh
      @Argosh 10 หลายเดือนก่อน +1

      The channel is called Josh tried coding, not that he succeeded.

  • @kasper369
    @kasper369 11 หลายเดือนก่อน +5

    Discovered Hono while back, So excited that i could not sleep. Tell me Hono.js vs Elysia.JS which

    • @tamicktom
      @tamicktom 11 หลายเดือนก่อน +6

      Elysia ❤

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน +1

      both are kinda goated

  • @nvsWhocares
    @nvsWhocares 11 หลายเดือนก่อน +1

    What about nextjs middleware on CF workers? Should work too, no? Maybe a test with typical middleware like clerk or next-intil. If that stuff runs on CF might be some serious Speed gains 🔥

  • @frackinfamous6126
    @frackinfamous6126 11 หลายเดือนก่อน +3

    Do a video building out backend in bun. My curl responses always smoke my command prompt and break the screen 😂. Elysia and Hono both great frameworks but I’m leaning towards Elysia.

  • @DhanushkaC
    @DhanushkaC 5 หลายเดือนก่อน

    Nice catch! Can you please share the code if possible? I would like to see how it is structured and connected.

  • @thatpenguin965
    @thatpenguin965 11 หลายเดือนก่อน

    youre channel is a dream come true. Where do you even find these lol

  • @heyitsfemzy
    @heyitsfemzy 11 หลายเดือนก่อน +3

    Can I not just host my NextJs app on Cloudflare pages? As far as I know, all your serverless functions are deployed as workers this way

    • @calebcook96
      @calebcook96 11 หลายเดือนก่อน +1

      That was my first thought because thats where Ive been hosting mine.

  • @sathishkannan6600
    @sathishkannan6600 11 หลายเดือนก่อน

    I think you can just add FastMode during built time. but conditionally exporting "edge". Adding the hono, only helps in treating the backend code just like a separate server.

  • @patrickbranchaud7711
    @patrickbranchaud7711 10 หลายเดือนก่อน

    Do you think cloudflare/next-on-pages gives a similar configuration and speed result? might be another comparison to do?

  • @MikaelXJohansson
    @MikaelXJohansson 11 หลายเดือนก่อน +1

    Try K6 performance testing for better statistics

  • @wanarchives
    @wanarchives 11 หลายเดือนก่อน +2

    why im getting Error: You cannot define a route with the same specificity as a optional catch-all route ("/" and "/[[...route]]"). ? i follow ur video instructions :(

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน

      might be a problem with the pages vs app router, if you have both at the same time (hono initialized with pages by default, I switched to app manually), this can happen

    • @wanarchives
      @wanarchives 11 หลายเดือนก่อน +1

      @@joshtriedcodingi use app router only and never done pages router

    • @KellenProctor
      @KellenProctor 11 หลายเดือนก่อน

      Per the hono docs, move /app/[[...route]] to /app/api/[[...route]]. That fixed it for me

  • @matthewbeardsley7004
    @matthewbeardsley7004 10 หลายเดือนก่อน

    It certainly seems like a nicer way than using route.ts files, even on small projects, that doesn't seem appropriate, yet alone something decent.

  • @dabbopabblo
    @dabbopabblo 11 หลายเดือนก่อน

    3 years into a project and I wish I didn't use this paradigm. Over time Ive been forced to start defining groups of related routes in their own files as the main.go file serving the main web server has grown to be over 23 thousand lines of code. Its not nearly as practical to edit or make changes to something so large with the latency the language server takes alone, compared to defining routes in their own files. Plus with golang there is no performance penalty for spreading the same module/package/app across multiple files because they are all compiled into one binary before the servers executed.

  • @Schisek
    @Schisek 11 หลายเดือนก่อน

    I think with this approach you are calling the nextjs route from a server component using fetch which gives some overhead. But I guess the App Router was invented to avoid these unnecessary fetches. So maybe a client component would be a better comparison

  • @grinsk3ks
    @grinsk3ks 11 หลายเดือนก่อน +5

    I don't understand why I see so much hono, while nobody mentions h3. H3 works everywhere with everything, while having 10 times the usage.

    • @khazixfangirl6087
      @khazixfangirl6087 11 หลายเดือนก่อน

      My guess would be more familiar API (feels like express)+ they add some interesting features

    • @wolfphantom
      @wolfphantom 11 หลายเดือนก่อน

      h3 and nitro are both great to work with

    • @CoryTheSimmons
      @CoryTheSimmons 11 หลายเดือนก่อน +1

      Immediately noticed "eventHandler" bloat. Otherwise looks identical.

    • @daphenomenalz4100
      @daphenomenalz4100 11 หลายเดือนก่อน +1

      Looks like express

    • @xmorse
      @xmorse หลายเดือนก่อน

      Spiceflow is also good

  • @daphenomenalz4100
    @daphenomenalz4100 11 หลายเดือนก่อน +3

    How to deploy the frontend seperately too? I just got to know about this 😅, searched it up couldn't find it. Could you pls tell

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน

      if its static you can port it to pretty much anywere, even if not most places support next builds out of the box like railway, amplify or netlify

  • @naylord5
    @naylord5 11 หลายเดือนก่อน +1

    Great timing Josh! Thank you for this useful video.
    Please I have a couple of rooky questions:
    - CF Workers can entirely replace backend part of a web app? because in the pricing panel I see "Up to 10ms CPU time per request" in free tier, so makes me wonder if is only for very light request / payload or if can handle PDF creations or batch database transactions..
    - I'm just getting familiar using Next.js for the API / backend part, can I reuse the endpoints generated by Vercel or CF workers to write lets say a native mobile app? It generates a URL that can hit with headers auth or so?
    Thank you so much in advance!

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน

      hi! Technically you can reuse the APIs, practically you'd normally use a separate backend. At my last job we used fastify as the backend that our iOS, android and web apps could all access and it worked great

    • @naylord5
      @naylord5 11 หลายเดือนก่อน

      @@joshtriedcoding Thank you for the insight 🙌

    • @twitchizle
      @twitchizle 11 หลายเดือนก่อน +2

      10ms cpu time is not low. Its more than enough. But i dont think you can run pdf generations on cf workers.

    • @naylord5
      @naylord5 11 หลายเดือนก่อน

      @@twitchizle thank you mate, definitely will give it a try and compare between workers and AWS lambdas

  • @virtual5754
    @virtual5754 11 หลายเดือนก่อน

    What about deploying it to normal vps? Cheap, performant and without cold starts ever

  • @hamditliba4298
    @hamditliba4298 3 หลายเดือนก่อน

    I tested hono js vs server action , with prisma and next js 14.2, it's just the same, note: don't use the runtime='edge' every where, it gonna make some bugs, who try drizzle?

  • @r3plican
    @r3plican 11 หลายเดือนก่อน

    are u not censorring env vars? in the minute 4:27 i can see the user and pass of ur database is

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน

      i just rotate passwords or delete databases before uploading

  • @kevboutin
    @kevboutin 17 วันที่ผ่านมา

    This test for the edge is seriously flawed. If your backend was hitting a cloud database (like MongoDB Atlas for example), then you would have more latency to the database on writes and reads. This would be a better test and would show that the edge is only better if all operations stay on the edge!

  • @MyCodingDiary
    @MyCodingDiary 11 หลายเดือนก่อน +2

    I've watched this video multiple times. Never gets old!

    • @tvnishq
      @tvnishq 11 หลายเดือนก่อน +1

      This was released yesterday bruh

    • @FeneelAPatel
      @FeneelAPatel 9 หลายเดือนก่อน

      @@tvnishq lol

  • @anasouardini
    @anasouardini 11 หลายเดือนก่อน

    Another awesome video 🔥

  • @robindeguzman8741
    @robindeguzman8741 11 หลายเดือนก่อน

    How to fix the build error, wherein the error shows if the APIs that are being fetch are from the NextJS API instead of external API?

  • @xtromp
    @xtromp 11 หลายเดือนก่อน +3

    Can it replace tRPC or it does not provide the same e2e typesafe story? How about validation/error handling? Thanks for pointing the Vercel overhead.

    • @kevboutin
      @kevboutin 17 วันที่ผ่านมา

      The protocol here should not matter.

  • @herzog0
    @herzog0 11 หลายเดือนก่อน +2

    Wtf is export default as never??

    • @samsupplee-niederman1752
      @samsupplee-niederman1752 11 หลายเดือนก่อน

      From my understanding, it’s telling typescript “I need to export this file, but I should never use/import it anywhere else”

  • @iury664
    @iury664 11 หลายเดือนก่อน

    what it means "run at the edge" i'm not english natural, so i didn't get it.

    • @tubero911
      @tubero911 11 หลายเดือนก่อน

      Hosted at a CDN edge location. Closer to the user, so lower latency.

  • @dev_generico
    @dev_generico 11 หลายเดือนก่อน

    Is it possible with trpc?

  • @AbegazNap
    @AbegazNap 11 หลายเดือนก่อน +5

    what was I saw, a new tcp connection being established to the database on every request, it is PHP all over again lmao

    • @farjulmallik4135
      @farjulmallik4135 11 หลายเดือนก่อน +2

      bro can you elaborate more on this !!!!....

    • @AbegazNap
      @AbegazNap 11 หลายเดือนก่อน

      @@farjulmallik4135 He is calling the function that establishes the connection inside the route handler which means a connection is being established on every request made to the route handler, this was mainly due to not being able to access the environment variable and not a runtime limitation, but this emulates what PHP does where it is completely stateless and the concept of database connection pooling or long lived connections do not exist because on every request php restarts its process. Most backends nowadays have connection pools to alleviate the issue of connection reestablishment. Not having pools or not having a long lived connection may not be the main concern of users of such a system, but there is a stark difference in terms of database IO performance between backends using a connection pool/long lived connection and those which do not.

    • @zoladkow
      @zoladkow 11 หลายเดือนก่อน

      yep, so now it's JS turn to solve those issues again :)

    • @twitchizle
      @twitchizle 11 หลายเดือนก่อน

      Pgbouncer

  • @mdsalahuddin2841
    @mdsalahuddin2841 11 หลายเดือนก่อน

    how did you managed avoiding ; (semi-colon) at the end code?

    • @exciting-burp
      @exciting-burp 11 หลายเดือนก่อน

      It's called automatic semicolon insertion. Very few people learn about it, so it ends up being confusing. It is nothing more than a toxic flex, don't use it.

    • @MikeBooger
      @MikeBooger 10 หลายเดือนก่อน

      Because what for?

  • @peternicholas4750
    @peternicholas4750 11 หลายเดือนก่อน

    Does this work with preview deployments?

  • @carlosricardoziegler2650
    @carlosricardoziegler2650 11 หลายเดือนก่อน

    Nice, I am using it and is amazing, fast and so simple :)

  • @MohammedHamim4444
    @MohammedHamim4444 6 หลายเดือนก่อน

    Josh, my brother, I really need your help with my backend architecture. shouldnt take that much amount of time for you but if you could help i will be really greatefull to you

  • @FedericoGobea
    @FedericoGobea 11 หลายเดือนก่อน

    Is this associated to the server? or to hono / next?

  • @kevinnguuyen
    @kevinnguuyen 11 หลายเดือนก่อน

    Does this work with nextAuth v4?

  • @genechristiansomoza4931
    @genechristiansomoza4931 11 หลายเดือนก่อน

    The fastest api is a "no code" api.

  • @mr-skorpion
    @mr-skorpion 11 หลายเดือนก่อน

    How big of a script, size wise, can you host on CF workers anyway?

  • @JinseokSeo-p8q
    @JinseokSeo-p8q 11 หลายเดือนก่อน

    Using Edge is not fundamentally writing faster api....

  • @sanampakuwal
    @sanampakuwal 11 หลายเดือนก่อน +4

    nextjs backend? emm.. dotnet backend, yes!!!

    • @JakobTheCoder
      @JakobTheCoder 11 หลายเดือนก่อน +7

      What is wrong with using Next.js as a backend framework? Isn't it nice that you can stay in the same language and framework and have it deployed the same way?

    • @spooky4655
      @spooky4655 11 หลายเดือนก่อน +1

      ​@@JakobTheCoder i think op is joking

    • @PieterWigboldus
      @PieterWigboldus 11 หลายเดือนก่อน +4

      @@JakobTheCoder If you are frontend developer, and only use it to connect to a single frontend.
      If you want to do more with your backend, Next.js is not a good choice.
      It is a backend for frontend, not a backend to create a generic API.
      Also if you want to build more workers / microservices, or want more freedom and not have vendor locking, Next.js is not what you want.
      So Next.js is great as frontend developer, and only for a single frontend with a single backend.

    • @sanampakuwal
      @sanampakuwal 11 หลายเดือนก่อน +1

      ​@@JakobTheCoder yes, great for small-medium size applications, but JavaScript's performance is not enough for large/perf centric applications.
      This is where C#, Java, Rust, and other languages shines!!

    • @joshtriedcoding
      @joshtriedcoding  11 หลายเดือนก่อน +1

      @@sanampakuwal There are good points to make for and against using JS / C# etc. on the backend, this is a great discussion

  • @gustavo-oi1dd
    @gustavo-oi1dd 11 หลายเดือนก่อน +11

    or just, saying, pick another language

    • @ak_mittal
      @ak_mittal หลายเดือนก่อน

      Cloudflare workers are fast because these are running closer to users location. Its not easy to build edge functions in a language other than JS.
      If you are running in the fastest language but still serving from one centralized location, that will be slower because of network latency

  • @3ladeZ
    @3ladeZ 11 หลายเดือนก่อน

    Wondering about hono vs elysia aswell

    • @PaulVarna
      @PaulVarna 11 หลายเดือนก่อน

      I prefer Elysia, but Elysia has some "Bun only" stuff, that doesn't work on Workers.

  • @lifewithyousof
    @lifewithyousof 11 หลายเดือนก่อน

    Banger

  • @AmodeusR
    @AmodeusR 9 หลายเดือนก่อน

    It seems Vercel just gave up on Edge, this won't be an option anymore. At least not with Vercel.
    th-cam.com/video/lAGE-k1Zfrg/w-d-xo.html

  • @PeterZhou-vi3ep
    @PeterZhou-vi3ep 11 หลายเดือนก่อน

    🎉🎉🎉

  • @k0in640
    @k0in640 11 หลายเดือนก่อน

    "how to make your api faster" use a faster service!
    - duh

  • @daphenomenalz4100
    @daphenomenalz4100 11 หลายเดือนก่อน

    I'll still prolly use express or rust for backend, but knowing this is pretty good for small scale applications that I don't want to spend much time on 👀

  • @nro337
    @nro337 11 หลายเดือนก่อน

    super cool!

  • @AkashLayal
    @AkashLayal 11 หลายเดือนก่อน

    hi josh i wanted to know how can we use prisma on cloudflare worker.

    • @Malix_Labs
      @Malix_Labs 10 หลายเดือนก่อน +2

      Use Drizzle instead

  • @venkatesheraser
    @venkatesheraser 11 หลายเดือนก่อน

    what is the name of the font?

  • @noext7001
    @noext7001 11 หลายเดือนก่อน +2

    can you make it work with trcp ?

    • @ephorUA
      @ephorUA 11 หลายเดือนก่อน

      yes, there is trpc middleware for Hono

    • @austincodes
      @austincodes 11 หลายเดือนก่อน

      You can make anything with with trpc

  • @coocasualvibes9901
    @coocasualvibes9901 11 หลายเดือนก่อน +1

    Even backend from Node Js is too vast and heavy for any software services

  • @sanyamjain7229
    @sanyamjain7229 10 หลายเดือนก่อน

    hey. You have 120K subscribers. you need to work up the game.
    (we all know this video was not up to the mark .)
    btw, vercel also supports edge functions.

  • @FranFiori94
    @FranFiori94 11 หลายเดือนก่อน

    Nah thanks

  • @joloppo
    @joloppo 11 หลายเดือนก่อน

    Stop deploying js backends 😂

  • @nnz13
    @nnz13 9 หลายเดือนก่อน

    15ms vs 30ms nobody cares

  • @PeterZhou-vi3ep
    @PeterZhou-vi3ep 11 หลายเดือนก่อน

    prisma do not work

    • @Malix_Labs
      @Malix_Labs 10 หลายเดือนก่อน +2

      That's because you don't use Drizzle

  • @Swerce0
    @Swerce0 11 หลายเดือนก่อน +1

    Please heart ❤

  • @Swerce0
    @Swerce0 11 หลายเดือนก่อน +1

    Last

  • @iogilarb
    @iogilarb 9 หลายเดือนก่อน

    what is this? 😅

  • @indrakhatiwada9860
    @indrakhatiwada9860 11 หลายเดือนก่อน

    First

  • @eliassmith7949
    @eliassmith7949 11 หลายเดือนก่อน +2

    stop making from nextjs freaking backend -_-

  • @DennisFeher
    @DennisFeher 11 หลายเดือนก่อน

    What ur doing is not programming. Children Playing coders 🤷‍♂️