NextJS 13 API Routes: Better Than Expected!

Поделиться
HTML-код
  • Опубликовано: 9 ноя 2024
  • In this video, we'll take a look at NextJS 13 API Routes, and how they're better than I originally expected! NextJS 13 is a major update to NextJS 12, and it introduces some great new features like easier-to-write API routes.
    We'll take a look at some example routes and see how easy it is to create your own. After watching this video, you'll be able to use NextJS 13 API routes to create your own new API endpoints.
    My GitHub: github.com/jos...
    Wishing you lots of fun building your own cool stuff with the new route handlers!

Комментарии • 96

  • @MrUltrakitsch
    @MrUltrakitsch Год назад +22

    Hi Josh! Just wanted to drop by and say that your latest video was amazing! You have a real talent for explaining things in a clear and concise manner, which is not an easy feat. Keep up the great work and I'm excited to see more of your content in the future!

  • @blaizeW
    @blaizeW Год назад +2

    Luckly I've discovered your channel a couple of days ago, and now i see how many useful things you post. Thank you.

  • @ohmegatech666
    @ohmegatech666 4 месяца назад +1

    Thanks this is some of the most up to date info on doing API routes and requests using the new app router in Next. Would love a video about passing cookies and headers along to emulate a browser session so you can log into another website and take some actions that would normally require a use to log in

  • @alexorr4212
    @alexorr4212 Месяц назад

    I appreciate you doing the cross comparison and using typescript in your demo. Most demos I find do not include the typescript

  • @jaceborg
    @jaceborg Год назад +7

    Thanks for the side by side comparison, easily the best explanation I've seen :)

  • @fluntimes
    @fluntimes 10 месяцев назад +1

    Enjoyed this video. Looking at the differences really helped as I'm getting back into Next after a few years away. I too was skeptical because the old way mirrored Express.js, which I like a lot.

  • @ariyoujahan9662
    @ariyoujahan9662 Год назад +1

    I like how short and useful this video is!

  • @saeedradmehr1976
    @saeedradmehr1976 Год назад +1

    New route handler is somewhat obscured in the documentation. Great job parsing it.

  • @steamfits7
    @steamfits7 Год назад

    Thank you for this tutorial. I just started learning next js apis. I worked on express js and before this video I was looking into old version of next js routing and it was not working. Thanks you again. Love from India!

  • @timothypark1037
    @timothypark1037 Год назад +3

    Thank you soo much man! Was having problems with what you pointed out on 5:51 ... Thanks for clearing things up!

  • @nicolasmayorga8288
    @nicolasmayorga8288 Год назад

    Hi Josh ! You can't imagine how this video saved me, really thanks you are the best and got a new subscriber.

  • @akashkallumkal
    @akashkallumkal Год назад +1

    this video is amazing, I finally figured out how to use the api!!

  • @ClementDERYCKX
    @ClementDERYCKX 7 месяцев назад

    Way too late, but thank you for your work. It was simple, not too fast, clear and most important, very useful.

  • @codewithguillaume
    @codewithguillaume Год назад

    Oh my goodness that's what I was looking for !
    We need to make a call josh

  • @cristhopersanchez2449
    @cristhopersanchez2449 Год назад

    You saved me, 13 is confusing. I was about to cry.

  • @harshilparmar9076
    @harshilparmar9076 Год назад

    Thanks brother!! Explaining concept by comparing with next 12 is the best way to learn I guess.

  • @dezo1604
    @dezo1604 Год назад

    als aller erstes möchte ich dir sagen, dass mir deine Videos sehr gefallen und ich sie gerne schaue.
    Ist also kein hate! Aber 2:30 ist so geil :D

  • @barehandstudio
    @barehandstudio 7 месяцев назад

    i only watched 2 mins and i saw my solution. you are awesome

  • @claytonmartins3761
    @claytonmartins3761 Год назад

    Thanks Josh! 😀

  • @faizanahmed9304
    @faizanahmed9304 Год назад +2

    Thank you Josh! Your videos are amazing!

  • @karimben1477
    @karimben1477 Год назад +1

    I’m fan of the old fashioned way to be honest.

  • @iFrancisDC
    @iFrancisDC 6 месяцев назад

    I'm curious to see how your page.tsx looks like where you defined the button

  • @ridoy51306
    @ridoy51306 6 месяцев назад

    Thank you so much!
    you literally answered all of my questions about the new app router api.
    I had one question about the server components
    for example in a login page, which is a server component:
    I have to to create a separate LoginForm component, use "use strict" at the top of the file and import it.
    it's kinda annoying because now even a core functionality of that login page has to be a separate client component as it needs to use useState.
    I could move that component to the login page, but since I have to use "use client" at the top of the file, I cant.
    so I am in a situation where any server component with some basic functionality needs to be extracted into a separated client component. I don't want to bloat my components folder for every server component page.
    any idea how can I define simple client components inside a server component file?

  • @roysheppard-dev
    @roysheppard-dev Год назад +1

    Thanks for the video I haven’t used apis in version 13 yet but found it frustrating in the past as adding middleware seemed so complicated and I would end up adding dependencies to make it work more like express so I currently am just using express. Would love it if they just let us add middleware in the handler options somehow

    • @joshtriedcoding
      @joshtriedcoding  Год назад

      Yeah. Another option besides just switching frameworks altogether was to develop custom middleware, for example only allowing specified request methods

  • @eddieuptheirons
    @eddieuptheirons Год назад

    I'm from Brazil! Thank you man!

  • @NaveedAli-n5g
    @NaveedAli-n5g 7 месяцев назад

    Thank you for such an amazing video, this was very helpful.

  • @ScriptCodec
    @ScriptCodec Год назад +1

    Great one bro. Please how do I basically upgrade from using handler to using http methods directly from next13? Simply how do I upgrade to the latest version?

  • @ae_holic
    @ae_holic Год назад

    What about the slugs and parameters/search parameters? Wish you had it covered in the video.

  • @cikplays7375
    @cikplays7375 Год назад

    thunder client extension for vs code is a great way to make/test api requests

  • @johndzikunu9986
    @johndzikunu9986 Год назад

    thank you very much for this

  • @utkarshsharma1185
    @utkarshsharma1185 8 месяцев назад

    Really nice and informative video, thanks

  • @zoki5388
    @zoki5388 Год назад +1

    Is it just me or are beta docs impossible to view in light theme?
    I've tried changing it in settings but it doesn't seem to work.

  • @TomAndGingeCoLtd1996
    @TomAndGingeCoLtd1996 10 месяцев назад

    Great video!

  • @AminEminosCr7
    @AminEminosCr7 11 месяцев назад

    Hello Josh thanks for the video, i have just one question if we use the Edge runtime can we deploy our app on a diffenet cloud hoster from Vercel like Amazong for example ? Sorry for this dummy question i am new to Edge Thank you

  • @kalpadhwaryu7929
    @kalpadhwaryu7929 Год назад

    Informative videos. Keep making them.

  • @amyIsFlexable
    @amyIsFlexable Год назад

    Thanks for doing this--this information is so hard to find/infer in the official docs. Is there a link to the github for this project?

  • @microspacer
    @microspacer Год назад

    Thank you for your effort!

  • @stephenlau2845
    @stephenlau2845 Год назад

    Hi, Thanks for your Tutorial, How can I use API Routes database to one to one and one to many ?

  • @tunaralyarzada
    @tunaralyarzada Год назад +1

    First of all ur contents are amazing brother, keep it up! I am just having trouble to get session object inside api routes, as returns null:( Do u know solution of it ?

  • @nityanandagohain4154
    @nityanandagohain4154 5 месяцев назад

    thank you

  • @kodekorp2064
    @kodekorp2064 Год назад

    I still can't understand why you think this is "cleaner", I do like the structure of the requests for more clarity, but all the new syntax seems to be writing a bit more additional characters for what we would already be writing previously.

  • @-iraq4374
    @-iraq4374 Год назад +15

    We need full stack projects 🤍

    • @joshtriedcoding
      @joshtriedcoding  Год назад +17

      If only you knew what I'm recording over the next days

    • @nubian_goat
      @nubian_goat Год назад +3

      Yes bro we really need it. Please make full stack blog app.

    • @blazi_0
      @blazi_0 Год назад

      عراقيييييي ❤❤😅🎉

    • @-iraq4374
      @-iraq4374 Год назад

      @@blazi_0 اييي 😂

  • @FemiAdigun
    @FemiAdigun Год назад +2

    Hi Josh, thanks for this. Do you have any idea why this is not in NextJS's official documentation? The examples on their doc page still use the old pages/api and will keep returning 404. The least we can do as a tech service provider is to update our docs and stop misleading the poor users. NextJS failed on this, and thanks for the video.

    • @AhmadMughal1
      @AhmadMughal1 Год назад +1

      i came back to NextJS after a long time and was struggling to why is it not working and nothing can be found related to this in the documentation aswell.

  • @johnsoto7112
    @johnsoto7112 11 месяцев назад

    running into the error Error: occurred prerendering page "/api/subway/nearest-station". when attempting to access the get request from from the api route. Any idea why this might be happening?

  • @jaymishra1781
    @jaymishra1781 Год назад

    Is it a good idea to learn Nextjs from old tutorial which was made in 2021 and it's NextJs 11....how much different is NextJs 14 from nextjs 11

  • @kuroisan2698
    @kuroisan2698 Год назад

    Josh I was wondering if You try to make to make a simple api for file upload in the new app directory
    bcs when I tried to use "formidable" library for parsing the upcoming data it shows me a weird error

    • @joshtriedcoding
      @joshtriedcoding  Год назад

      I did that in the image-to-alt open source project here: github.com/joschan21/image-alt-generator, we're uploading the images via a signed url directly to AWS S3

  • @AnisKhan-pq1iq
    @AnisKhan-pq1iq Год назад

    Hello bro I am facing an issue where res data is displayed correctly in the console and Postman, but when I make a request, it shows 'id' as undefined in both the console and Postman. My folder structure is app api than user in user [id] route.js

  • @matarloum2894
    @matarloum2894 Год назад

    contentlayer build seems not working with Next Js 13.2 or more

  • @8koi245
    @8koi245 Год назад

    It really changed a lot lmao, was trying to make it from memory and nothing was working hahahahah 1min in I got it right

  • @oumar_kone
    @oumar_kone Год назад

    Thanks you

  • @timekouka
    @timekouka Год назад

    awesome bro

  • @rohitshrivastava4318
    @rohitshrivastava4318 Месяц назад

    You look like Andy's neighbour from Toy Story 1

  • @Code2bsmart
    @Code2bsmart Год назад

    thankyou

  • @Jack-hk6kl
    @Jack-hk6kl Год назад

    Got any videos that talk abt how slow next js is in compile time and how to remedy it?

  • @horacioserrano5430
    @horacioserrano5430 9 месяцев назад

    how can i send a raw html string in the body, so that it is read as html and not encapsulated inside a "pre" tag?

  • @birdsonlybirds8291
    @birdsonlybirds8291 Год назад

    how come you can call the api using a relative path "/api/user"? When i tried it, i got "unable to parse URL"; I had to use the absolute path

  • @hongyang5769
    @hongyang5769 6 месяцев назад

    ure goat

  • @AE-qj5xi
    @AE-qj5xi Год назад

    Thank you! the directory really confuse me

  • @MuhammadBilal-e5g5n
    @MuhammadBilal-e5g5n Год назад +1

    Why I am not able to make relative api call without localhost in the url?

    • @joshtriedcoding
      @joshtriedcoding  Год назад

      Chances are you're trying to make that request from the server side

    • @MuhammadBilal-e5g5n
      @MuhammadBilal-e5g5n Год назад

      @@joshtriedcoding Yes I have to do that. because I am using drizzle ORM through route handlers. cuz only then it fetches the static response from the db. I don't know how to make db call in RSC and make that page static

  • @mojojojo6525
    @mojojojo6525 5 месяцев назад

    Great video
    The docs don't explain this

  • @PreyzPlayer
    @PreyzPlayer Год назад

    Wäre cool wenn du mal T3 Stack zeigen und erklären würdest

  • @itsabun
    @itsabun Год назад

    Hi josh how to POST a request that body have a file type?

  • @hazeldinn
    @hazeldinn 3 месяца назад

    how we can customize the status code?

  • @makisetakashi
    @makisetakashi Год назад

    app/api cant use req.body so it force api request to send json data only, that bad side

  • @timekouka
    @timekouka Год назад

    guys, do the old way of making API routes works? i am trying to get the preview mode with sanity and the documentation seems broken since that next13 release

    • @joshtriedcoding
      @joshtriedcoding  Год назад

      the old ones work the same way, you can still do all the regular api stuff in the pages directory for incremental adoption

  • @KRISHNU10
    @KRISHNU10 Год назад

    How do i make a nextjs backend autoscale on AWS ?

  • @gualterfernandes5413
    @gualterfernandes5413 Год назад

    but its not possible set the cookies in our api version? you only showed how to get() not how to set().

  • @amkamn1886
    @amkamn1886 11 месяцев назад

    how cookie set new api route ?

  • @obiaboimmanuel8729
    @obiaboimmanuel8729 Год назад

    this is running on Edge

  • @Zechey
    @Zechey Год назад

    I kind of hate how they do these huge changes and all the rest help on the internet becomes obsolete.. Like I have no idea how to set up cors for this now, having res as NextResponse and using res.headers.set to set the headers does not seem to work

  • @azizoid
    @azizoid Год назад

    I spent a week to transfer my pages router to the app router, and guess what, it is sooo much slower. Often new was ment to be better and faster. Nextjs, app router is better, but so much slower

  • @hartono9583
    @hartono9583 Год назад

    i also tried coding hoho

  • @mlnima
    @mlnima 8 месяцев назад

    really? I came to see just how to get the damn query from request which is the first thing and you injected me with later use ones

  • @directornovela7774
    @directornovela7774 Год назад

    you found out late

  • @nishantmogha7550
    @nishantmogha7550 Год назад

    The app directory is just a complete ripoff of sveltekit

  • @PlsDontSuuue
    @PlsDontSuuue Год назад

    Please install the icons extension, this tutorial was confusing as hell

  • @tubebility
    @tubebility Год назад

    LISTEN! 🤡
    Worst first-impression ever 😂🤣😂🤣

  • @loveboat
    @loveboat Год назад

    Another "upgrade" that is a thousand times worse than how things were done in NExtjs12. You have to put in a lot more boilerplate code and I still don't know how to return and get responses. Your way is erroneous.

  • @mateuspaula_dev
    @mateuspaula_dev Год назад

  • @alsherifkhalaf7385
    @alsherifkhalaf7385 Год назад

    I tried to upload file , i thought it its standard web api to use formData ,
    but i think the api route handlers parse data or change it :
    import { Directus } from '@directus/sdk';
    import { NextResponse } from 'next/server';
    export async function POST(request: Request) {
    const formData = await request.formData();
    const file = formData.get('file');
    formData.append('folder', '887b5198-6b28-4289-8117-87deb4df5e71');
    formData.append('file', file as File);
    console.log("form data", formData);
    const token: string = process.env.DIRECTUS_JOB_TOKEN!;
    const url: string = process.env.DIRECTUS_URL!;
    const directus = new Directus(url);
    await directus.auth.static(token);
    const fileResponse = await directus.files.createOne(formData);
    return NextResponse.json({ "message": "File Uploaded" });
    }
    the Axios error :
    [AxiosError: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream]
    even if i tried this on client side it works fine :
    const handleSubmit = async (event: React.FormEvent) => {
    event.preventDefault();
    const form = new FormData(event.currentTarget);
    console.log(form)
    // this using api route handler
    // const response = await fetch('/api/job', {
    // method: 'POST',
    // body: form,
    // })
    // console.log(response)
    const directus = new Directus('example.com/');
    await directus.auth.static('secretkeys');
    form.append('folder', '887b5198-6b28-4289-8117-87deb4df5e71');
    const file = form.get('file');
    if (file instanceof Blob) {
    form.append('file', file);
    } else {
    throw new Error('Invalid file data');
    }
    const fileRes = directus.files.createOne(form)
    .then(async (Response) => {
    return await Response?.data.id;
    })
    .catch(error => {
    console.error(error);
    });
    console.log(fileRes)
    };
    why is that ? is api route handlers parsing or changing the formData ?