This is the code I tried to execute using the help of documentation at node-pty and the sample code. I want to send the data received at ls as a response json object.
app.post("/try",(req,res)=>{
command=req.body.command
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
var ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 100,
rows: 100,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.onData((data) => {
res.status(200).json({
"Status":"Success",
"Output":data
})
process.stdout.write(data);
});
ptyProcess.write(`${command}\r`);
ptyProcess.resize(100, 40);
})
It gives me the following error:
node:_http_outgoing:663
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:399:5)
at ServerResponse.setHeader (node:_http_outgoing:663:11)
at ServerResponse.header (/home/sahanaramesh/POC/backend/node_modules/express/lib/response.js:794:10)
at ServerResponse.send (/home/sahanaramesh/POC/backend/node_modules/express/lib/response.js:174:12)
at ServerResponse.json (/home/sahanaramesh/POC/backend/node_modules/express/lib/response.js:278:15)
at /home/sahanaramesh/POC/backend/server.js:48:25
at EventEmitter2.fire (/home/sahanaramesh/POC/backend/node_modules/node-pty/lib/eventEmitter2.js:40:22)
at PipeSocket.<anonymous> (/home/sahanaramesh/POC/backend/node_modules/node-pty/lib/terminal.js:85:61)
at PipeSocket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12) {
code: 'ERR_HTTP_HEADERS_SENT'
}
Node.js v18.14.0
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
I tried to execute a ls command as Express POST request through node-pty. I wanted the resulting data to be sent as a response.json