Skip to content

Instantly share code, notes, and snippets.

@clinyong
Created September 24, 2023 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clinyong/f94c0108e86b4f0a8ed6088b34c5c7f3 to your computer and use it in GitHub Desktop.
Save clinyong/f94c0108e86b4f0a8ed6088b34c5c7f3 to your computer and use it in GitHub Desktop.
Using Next.js to fetch an audio file and respond it to the client
import { NextResponse } from "next/server";
async function GET(
_request: Request,
{ params }: { params: { url: string } }
) {
// An audio url, for example, https://xxx/xxx.mp3
const audioUrl = decodeURIComponent(params.url);
const headers = new Headers();
const res = await fetch(audioUrl).then((res) => {
headers.set("Content-Type", res.headers.get("Content-Type")!);
headers.set("Content-Length", res.headers.get("Content-Length")!);
return res.body;
});
return new NextResponse(res, { status: 200, headers });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment