node.js - 关于如何从另一个 nodejs 脚本调用无服务器 shell 命令的任何想法
问题描述
我正在尝试调用无服务器命令,例如
serverless invoke local -f hello
但来自另一个nodejs脚本。我使用节点 child_process spawn 对其进行测试
const {spawn} = require ('child_process');
const cmd = 'serverless invoke local -f hello';
const p = spawn (cmd, [], {shell: true});
p.stdout.on ('data', (data) => {
console.log (data.toString ());
});
或使用 exec
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function main() {
const { stdout, stderr } = await exec('serverless invoke local -f hello');
if (stderr) {
console.error(`error: ${stderr}`);
}
console.log(`${stdout}`);
}
main()
当我从终端运行这两种解决方案时
节点 myscript.js
我无法得到任何回应,任何关于如何做到这一点的建议或想法都会非常有帮助
解决方案
代码:
const { spawnSync } = require('child_process');
const child = spawnSync('serverless', ['invoke', 'local', '-f', 'hello'])
console.log(child.output.toString('utf8'))
回复:
,{
"statusCode": 200,
"body": "{\n \"message\": \"Go Serverless v1.0! Your function executed successfully!\",\n \"input\": \"\"\n}"
}
,
推荐阅读
- bash - 如果值可以为空,如何在 bash 中读取 key=value 对流?
- wordpress - WooCommerce 中的动态进度条
- wpf - 具有已定义列和行的网格的 WPF 样式?
- hyperledger-fabric - Hyperledger Fabric:Java 类中的对等链码实例化错误
- javascript - 如何将二进制流转换为其十六进制表示(或 UTF8)
- r - 如何根据另一个数据框更新一个数据框中的列?
- css - 响应式html:包装时在主列中的元素之间放置侧列
- reactjs - 与 chrome 和 safari 相比,firefox 滚动到动画非常滞后
- javascript - 如何解决 Ramda reduce 函数的错误返回结果(javascript)
- python - 无法使用 Python 将 CSV 数据导入 MySQL 数据库