如何在 Koa 中使用 child_process.exec (async/await)

首先安装 child-process-promise

install.sh
npm i --save child-process-promise

然后你可以像这样使用它:

koa_exec.js
const router = require('koa-router')();
const {exec} = require('child-process-promise');

router.get('/test', async ctx => {
  const [stdout, stderr] = await exec('python myscript.py')
  const ipv6 = stdout.toString();
  ctx.body = ipv6;
});

Check out similar posts by category: Javascript