Next.js Discord

Discord Forum

I can't seem to find anything, but Can I load a random image from an API route?

Unanswered
White-tailed Tropicbird posted this in #help-forum
Open in Discord
White-tailed TropicbirdOP
export async function GET(_: Request): Promise<ImageResponse> {
    const images: string[] = readdirSync(join(__dirname, "..", "public", "hasbullas"));
    const randomImage = (await import(images[Math.random() * images.length])).default
    return new ImageResponse(randomImage);
}


I tried this in the first time, but this does not work, I want to get an image from whatever folder I can store them, a random image from that folder

0 Replies