Unexpected 404 Response Despite Correct Terminal Output Logging #1891
-
I recently transitioned from Code for that endpoint app.post("/execute-command", async (c, next) => {
try {
const { command } = await c.req.json();
if (!command) {
return c.json({ error: "Command is required." }, 400);
}
// Execute the command using node-pty
const term = pty.spawn(
process.platform === "win32" ? "cmd.exe" : "bash",
["-c", command],
{
name: "xterm-256color",
cols: 80,
rows: 24,
cwd: process.platform === "win32" ? undefined : process.env.HOME,
}
);
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|");
const ansiRegex = new RegExp(pattern, undefined);
const ansiRegex2 = new RegExp(pattern, "g");
let output = "";
// Listen for data events (output from the command)
term.onData((data) => {
output += data;
});
// Listen for the process to exit
term.onExit(() => {
console.log(output: ansiRegex.test(output)
? output.replace(ansiRegex2, "")
: output)
// Send the parsed output back to the client
return c.json({
output: ansiRegex.test(output)
? output.replace(ansiRegex2, "")
: output,
});
});
await next()
} catch (error) {
console.error(error);
return c.json({ error: "Failed to execute command" }, 500);
}
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
What is the result you want when |
Beta Was this translation helpful? Give feedback.
-
I have tried how you have mentioned but it returns the dispose function instead of the output |
Beta Was this translation helpful? Give feedback.
new Promise((resolve, reject) => {
...
term.onExit(() => {
...
resolve(result)
})
})