Update server.js
Browse files1234567891011121314151617181920212223242526272814293031323334353637383940411617181942434445464748495022515253242554555657282958596061323362636465666768697071387273const express = require("express");const { spawn } = require("child_process");const path = require("path");
const app = express();app.use(express.json());
const PORT = process.env.PORT || 7860;
/** * Health check (important for HF) */app.get("/", (req, res) => { res.send("✅ Moltbot is running on HF Space");});
app.get("/health", (req, res) => { res.json({ ok: true });});
/** * Run Moltbot * Body: * { * "args": ["--help"] * } */app.post("/run", (req, res) => { const args = req.body.args || []; const args = Array.isArray(req.body?.args) ? req.body.args : [];
let moltbotEntry; try { // Resolve Moltbot's actual JS entry file moltbotEntry = require.resolve("moltbot"); } catch (err) { return res.status(500).json({ error: "Failed to resolve Moltbot entry file", details: err.message }); }
const moltbot = spawn( "npx", ["moltbot", ...args], { shell: true } const child = spawn( "node", [moltbotEntry, ...args], { cwd: process.cwd(), env: process.env } );
let output = ""; let stdout = ""; let stderr = "";
moltbot.stdout.on("data", data => { output += data.toString(); child.stdout.on("data", (data) => { stdout += data.toString(); });
moltbot.stderr.on("data", data => { output += data.toString(); child.stderr.on("data", (data) => { stderr += data.toString(); });
moltbot.on("close", () => { res.json({ output }); child.on("close", (code) => { res.json({ exitCode: code, stdout, stderr }); });});
app.listen(PORT, "0.0.0.0", () => { console.log(`🚀 Moltbot server listening on ${PORT}`); console.log(`🚀 Moltbot server listening on port ${PORT}`);});server.js+47-13