Skip to content

Commit

Permalink
feat: send stderr as response
Browse files Browse the repository at this point in the history
  • Loading branch information
phukon committed Mar 28, 2024
1 parent f0ab7b9 commit a930f4e
Show file tree
Hide file tree
Showing 5 changed files with 1,150 additions and 13 deletions.
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const express = require('express');
const { spawn } = require('child_process');

const app = express();

app.use(express.text()); // Parse text/plain requests

app.post('/', (req, res) => {
const command = req.body.trim();
console.log(`Received command: ${command}`);

const shellCommand = process.platform === 'win32' ? 'cmd' : 'bash';
const child = spawn(shellCommand);

child.stdin.write(command + '\n');
child.stdin.end();

let stderrData = ''; // Store stderr data

// Capture stdout
child.stdout.on('data', (stdoutData) => {
res.write(stdoutData);
});

// Capture stderr
child.stderr.on('data', (stderrChunk) => {
stderrData += stderrChunk.toString(); // Append stderr data
});

child.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
res.end(stderrData); // Send stderr data as part of response
});

child.on('error', (error) => {
console.error(`Error spawning child process: ${error.message}`);
res.status(500).send(`Error spawning child process: ${error.message}`);
});
});

app.get('/', (req, res) => {
res.send('Server running');
});

const PORT = 8080;
app.listen(PORT, () => {
console.log(`HTTP server running on port ${PORT}`);
});
Loading

0 comments on commit a930f4e

Please sign in to comment.