Next.js Discord

Discord Forum

How to server Images uploded outside public folder

Unanswered
Nitinchowdary posted this in #help-forum
Open in Discord
I'm working with next.js for long time the only issue i see is how to serve images from outside the public folder. Can you suggest any library that handles this easily.

1 Reply

@Nitinchowdary I'm working with next.js for long time the only issue i see is how to serve images from outside the public folder. Can you suggest any library that handles this easily.
something like this?

export async function GET() {
  const imagePath = join(process.cwd(), 'public', 'images', 'example.png');
  const imageBuffer = await readFile(imagePath);

  return new Response(imageBuffer, {
    status: 200,
    headers: {
      'Content-Type': 'image/png',
      'Cache-Control': 'public, max-age=31536000, immutable',
    },
  });
}