关于如何从另一个节点 js 脚本调用无服务器 shell 命令的任何想法
Any ideas on how to call serverless shell comand from another nodejs script
我正在尝试调用无服务器命令,例如
serverless invoke local -f hello
但来自另一个 nodejs 脚本。
我使用 node 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()
当我 运行 来自我的终端的两个解决方案都是
node 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}"
}
,
我正在尝试调用无服务器命令,例如
serverless invoke local -f hello
但来自另一个 nodejs 脚本。 我使用 node 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()
当我 运行 来自我的终端的两个解决方案都是
node 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}"
}
,