Created
September 24, 2023 01:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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