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