nextjs-cmd / app /api /run /route.ts
diamond-in's picture
Update app/api/run/route.ts
6ff0d27 verified
import { NextResponse } from 'next/server';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
export async function POST(req: Request) {
try {
const { command } = await req.json();
if (!command) {
return NextResponse.json({ error: "No command provided" }, { status: 400 });
}
// SECURITY WARNING: This allows executing ANY command on the container.
// Ideally, for a real app, you would whitelist commands here.
// Execute the custom command in the shell
// timeout: 10000ms (10 seconds) to prevent hanging
const { stdout, stderr } = await execAsync(command, { timeout: 10000 });
return NextResponse.json({
output: stdout || stderr, // Return whichever has content
type: stderr ? "stderr" : "stdout"
});
} catch (error: any) {
// If the command fails (e.g. syntax error in shell), return that
return NextResponse.json({
output: error.message || error.stderr || "Unknown Error",
type: "error"
});
}
}