FIRST LOOK! Next.js App Directory API Routes! (Canary Build, Brand New!)

Поделиться
HTML-код
  • Опубликовано: 10 ноя 2024
  • НаукаНаука

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

  • @0xtz_
    @0xtz_ Год назад +3

    PHP vibes, I really like the server component concept 👌🏼

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

      Everything goes back to the server eventually, lol. That being said, doing renders at the component level (instead of the whole page) is very powerful and unlocks a lot of cool things.

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

    Despues de mucha investigacion, este video fue el que nesesitaba para por fin entender como funcionaban las api routes en el nuevo directorio app. thanks you so much

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

    req.body won't work in app/api folder. but it will work in page/api

    • @ethan_mick
      @ethan_mick  Год назад +4

      Right, the request body is a readable stream; which you can use, but isn't super helpful: developer.mozilla.org/en-US/docs/Web/API/Request
      The methods `.json()`, `.formData()`, or `.text()` (there are some others) are the helpful "awaitable" methods that return the parsed data for you.

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

    Amazing content, I was wondering if I have models and API made in C#, this NEXTjs API Routes approach should not be used, correct? This would only be beneficial if you set up your models and schemas locally and use this talk to your database?

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

      Yes, that's correct. If you have another service setup that is your API, Models, and connects to the database then your frontend application (Next.js or not) can connect directly to that API.
      This is most useful when you have a Next.js app first and foremost and want to expose an API for another consumer (mobile app, perhaps). If you already have an API that can do that, you don't need these routes.
      Note that sometimes you might want to do something specific to your frontend app that you don't really need a full API for (perhaps a feedback widget that makes a Slack hook call, or a analytics integration). In that case you can build a quick API handler in Next.js and not pollute your main API service. Up to you!

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

      @@ethan_mick ohh thats a cool idea, I was not aware of combining different API's if you need something that's separate from your main API

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

    nice 👍

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

    Ethan - on the post side I am having a challenge actually reading JSON from a post'ed body. I've tried the old ways of destructuring the request.body and I am getting a type error because request.body is a readable stream? What? Do you have a full working post example that gets data from the body? Ideally I'd love it if this worked:
    ```js
    export async function POST(request: NextRequest) {
    const { name, author } = request.body
    const result = await prisma.book.create({
    data: {
    id: uuidv4(),
    name: name,
    author: author,
    },
    });
    return NextResponse.json(result);
    }
    ```

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

      Hello! To read the request body, you need to `await request.json()`. Make sure you're sending the data as JSON as well or else the Next.js parsing won't be able to read it. Here's a full endpoint: gist.github.com/ethanmick/ba361e93d1233ad9214af2b7228fb950

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

      @@ethan_mick Brilliant! That works perfectly, thanks so much.