Код: Выделить всё
export async function GET(request: NextRequest) {
const client = new ftp.Client();
client.ftp.verbose = true;
try {
console.log("Connecting to FTP server...");
await client.access(ftpConfig);
const remoteFilePath = "/public_html/images/data/gallery.json";
const chunks: Uint8Array[] = [];
const writableStream = new Writable();
writableStream._write = () => {};
writableStream.on("data", (chunk) => {
chunks.push(chunk);
});
console.log(`Downloading file from: ${remoteFilePath}`);
await client.downloadTo(writableStream, remoteFilePath);
console.log("File downloaded successfully.");
const fileContent = Buffer.concat(chunks).toString("utf8");
console.log("File content:", fileContent);
const jsonData = JSON.parse(fileContent);
console.log("File downloaded successfully.");
return NextResponse.json({ success: true, data: jsonData, message: "File downloaded successfully." });
} catch (error) {
console.error("FTP connection error:", error);
return NextResponse.json({ success: false, message: "FTP connection error." });
} finally {
client.close();
}
}
< /code>
На стороне клиента у меня есть это: < /p>
useEffect(()=>{
const fetchImagesData = async () => {
const response = await fetch("api/galleryData")
const data = await response.json()
}, [])
Подробнее здесь: https://stackoverflow.com/questions/795 ... rom-an-api