I'm currently working on a project using Next.js and storing files in the root directory under /uploads/qr/myimage.png
. How can I access this file within the project? I attempted to create a route /api/getImg/route.js
dedicated to serving image files, and included the following code:
import fs from 'fs';
import { NextResponse } from 'next/server';
import path from 'path';
export async function GET(req) {
const { searchParams } = new URL(req.url);
const id = searchParams.get("id");
const imagePath = path.join(process.cwd(), 'uploads', 'qr', `${id}.png`);
try {
const data = fs.readFileSync(imagePath);
return NextResponse.send(data);
} catch (error) {
console.error(error);
return NextResponse.send('Image not found');
}
}
Unfortunately, I encountered an error that reads:
next_dist_server_web_exports_next_response__WEBPACK_IMPORTED_MODULE_1__.default.send is not a function