首页 > 解决方案 > NodeJS 捕获 NODEJS 命令的输出并显示到表达窗口

问题描述

注意:我不想捕获 bash 命令的输出,而是像这样的命令:

console.log(1)

它只是在我的快速服务器上说 1 而不是将其记录到控制台

我对 NodeJs 非常陌生,但精通 python

这是我的代码:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send(req.param('cmd')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

如果我这样做,我想要:

prtc://my.server?cmd=console.log(1)

该页面将显示 1。
我该怎么做?

标签: node.jsexpress

解决方案


我自己用 PHP 做的。

对于任何需要代码的人,请在 REPL.IT 或 HEROKU 或类似网站上运行它。

<?php
$lang = $_GET['lang'];
$command = $_GET['cmd'];

if ($lang == 'sh') {
  System($command);
};
if ($lang == 'python3') {
  System('python3 -c "'.$command.'"');
};
if ($lang == 'python2') {
  System('python2 -c "'.$command.'"');
};
if ($lang == 'node14') {
$fp = fopen('cmds.js', 'w');
fwrite($fp, $command);

System('node cmds.js');
unlink('cmds.js');
};
?>

推荐阅读