Next.js Discord

Discord Forum

Is there any way for using server-to-server communication?

Unanswered
Red wood ant posted this in #help-forum
Open in Discord
Avatar
Red wood antOP
I'm glad to see you.
I have a problem that have been bothering me.
In this code, I can't use fetch to use ytdl-core library.
Therefore I have to find ways to use server-to-server communication.
Is there any way for using server-to-server communication?
Image

2 Replies

Avatar
import { NextResponse } from "next/server";
import ytdl from "ytdl-core";

export async function GET() {
  const musicStream = ytdl("");

  const data = new ReadableStream({
    start(controller) {
      musicStream.on("data", (chunk: Buffer) =>
        controller.enqueue(new Uint8Array(chunk))
      );
      musicStream.on("end", () => controller.close());
      musicStream.on("error", (error: NodeJS.ErrnoException) =>
        controller.error(error)
      );
    },
    cancel() {
      musicStream.destroy();
    },
  });

  const filename = "example.mp4";

  return new NextResponse(data, {
    headers: {
      "content-disposition": `attachment; filename=${filename}`,
    },
  });
}
Avatar
Red wood antOP
Oh, Thanks.
However I found different way to fix that before.
It is not using NextResponse, but using Response.
This is my code.
  try {
    if (url) {
      const stream = ytdl(`https://youtube.com/watch?v=${url}`, { filter: 'audioonly', quality: 'highestaudio', format: 'mp3' })
      const response = new Response(stream);
      return response
    }
    throw "no datas"
  } catch (e) {
    return NextResponse.json({ err: e }, { status: 500 })
  }