presenton/servers/nextjs/app/api/read-file/route.ts
2025-06-23 15:13:04 +05:45

19 lines
No EOL
527 B
TypeScript

import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
export async function POST(request: Request) {
try {
const { filePath } = await request.json();
const normalizedPath = path.normalize(filePath);
const content= fs.readFileSync(normalizedPath, 'utf-8');
return NextResponse.json({ content });
} catch (error) {
console.error('Error reading file:', error);
return NextResponse.json(
{ error: 'Failed to read file' },
{ status: 500 }
);
}
}