gpt4 book ai didi

node.js - 从 Nodejs 运行多个 Windows 命令

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:37 25 4
gpt4 key购买 nike

当我的Windows服务器收到post请求时,我需要它来执行命令:

cd D:\project
git pull
mvn clean compile
cd target
java -jar app.jar -argument

我写了代码,但它不起作用:

const nodeCmd = require('node-cmd')

app.post((req, res) => {
let command = `cd D:\project\my_project
git pull
mvn clean compile
cd target
java -jar app.jar ${req.body.arg}`;
nodeCmd.get(command, (err, data, stderr) => {
if(data) {
return res.json(data);
}
return err;
});
})

这是错误消息:

{ Error: Command failed: cd D:projectmy_project && dir
The system cannot find the path specified.

at ChildProcess.exithandler (child_process.js:294:12)
at ChildProcess.emit (events.js:198:13)
at maybeClose (internal/child_process.js:982:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
killed: false,
code: 1,
signal: null,
cmd: 'cd D:Tempautobooker && dir' }

最佳答案

命令字符串中的反斜杠不会被转义。您可以使用string.raw指示 JS 引擎将模板文字视为原始字符串。

let command = String.raw`cd D:\project\my_project
git pull
mvn clean compile
cd target
java -jar app.jar ${req.body.arg}`;

关于node.js - 从 Nodejs 运行多个 Windows 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com