Next.js Discord

Discord Forum

API route handler (app) not returning response (image) as expected

Unanswered
Broad-snouted Caiman posted this in #help-forum
Open in Discord
Broad-snouted CaimanOP
im providing an api route to access image on public so i make this an api but i have no idea why the code didnt execute my image response, as you can see my console on 3333 is reached, pls advice, thanks


import fs from "fs";
import path from "path";
import { NextResponse } from "next/server";

export async function GET(
request: Request,
{ params }: { params: { type: string; file: string } }
) {
try {
const imagePath = path.join(
process.cwd(),
"public",
"upload",
"images",
"1710290950686.jpg"
);

fs.readFile(imagePath, (err, data) => {
console.log(1111);

if (err) {
console.log(2222);
console.error("Error reading image file:", err);

return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}

console.log(333);

return new Response(data, {
status: 200,
headers: {
"Content-Type": "image/jpg",
"x-version": "123",
name: "rick",
},
});
});

return new Response("asd");
} catch (error) {
console.error("Error handling request:", error);
}
}

0 Replies