gpt4 book ai didi

node.js - Node.js 中的 execFile 不执行 Windows 命令

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

我的目的是运行 Windows .EXE。为了进行测试,我首先尝试一些简单的 Windows 命令。但看起来 execFile 只执行 Linux 命令。例如,如果我说执行“DIR”,它就会执行。但如果我执行“CD”,它就不会。然后如果我执行“pwd”就可以了。

我已阅读有关 execFile 的文档,但这并没有解决此问题。这是我的代码片段:

var user_input = req.body.name;
var command_array = user_input.split(/\s+/);
var command = command_array.shift();
var params = command_array;
console.log("Client Command: " + command);
console.log("Client Arguments: " + params);

var execFile = require('child_process').execFile

// this launches the executable and returns immediately
var child = execFile(command, params,
function (error, stdout, stderr) {
. . .
});

有人可以帮我解决这个问题吗?

这是输出:

W:\Dropbox\DSI (His)\1830_GUI\EddyServer>npm run-script devstart

> eddyserver@0.0.0 devstart W:\Dropbox\DSI (His)\1830_GUI\EddyServer
> nodemon ./bin/www

[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www`
GET / 200 586.609 ms - 882
GET /stylesheets/style.css 304 2.358 ms - -
GET /run/create 200 51.934 ms - 1135
GET /stylesheets/style.css 304 1.538 ms - -
Client Command: cd
Client Arguments:
Program execution failed. Error (if any):

POST /run/create 200 78.827 ms - 37
GET /run/create 200 46.997 ms - 1135
Client Command: dir
Client Arguments:
Here is the complete output of the program:
[ 'EddyServer\t\t app.js node_modules\t public write.txt\nNew\\ Text\\ Document.txt bin\t package-lock.json routes\nREADME.md\t\t controllers package.json\t views\n' ]
POST /run/create 200 410.113 ms - 180
GET /run/create 200 40.355 ms - 1135
GET / 304 68.436 ms - -
GET /stylesheets/style.css 304 2.428 ms - -
GET /run/create 304 41.589 ms - -
GET /stylesheets/style.css 304 1.851 ms - -
Client Command: pwd
Client Arguments:
Here is the complete output of the program:
[ '/w/Dropbox/DSI (His)/1830_GUI/EddyServer\n' ]
POST /run/create 200 300.082 ms - 46
GET /run/create 200 40.383 ms - 1135
Client Command: cd
Client Arguments:
Program execution failed. Error (if any):

POST /run/create 200 11.053 ms - 37
GET /run/create 200 26.313 ms - 1135
Client Command: ls
Client Arguments:
Here is the complete output of the program:
[ 'EddyServer\nNew Text Document.txt\nREADME.md\napp.js\nbin\ncontrollers\nnode_modules\npackage-lock.json\npackage.json\npublic\nroutes\nviews\nwrite.txt\n' ]
POST /run/create 200 62.154 ms - 157
GET /run/create 200 40.479 ms - 1135
Client Command: ls
Client Arguments: -la
Here is the complete output of the program:
[ 'total 213\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:39 .\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 08:45 ..\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:20 .git\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:20 EddyServer\n-rw-r--r-- 1 eddyq 197121 0 Nov 23 18:42 New Text Document.txt\n-rw-r--r-- 1 eddyq 197121 999 Nov 6 16:47 README.md\n-rw-r--r-- 1 eddyq 197121 1638 Nov 6 16:47 app.js\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:20 bin\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:20 controllers\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 08:48 node_modules\n-rw-r--r-- 1 eddyq 197121 76014 Nov 6 16:50 package-lock.json\n-rw-r--r-- 1 eddyq 197121 499 Nov 6 16:50 package.json\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:39 public\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:39 routes\ndrwxr-xr-x 1 eddyq 197121 0 Nov 24 07:39 views\n-rw-r--r-- 1 eddyq 197121 15 Nov 23 19:25 write.txt\n' ]
POST /run/create 200 223.792 ms - 901
GET /run/create 200 26.539 ms - 1135

最佳答案

这是因为 execFile 旨在执行文件,例如您的 EXE,这不是打开 shell。如果你想执行像 cd 这样的命令,你应该使用 exec 函数。pwd 适合您,而 cd 则不太可能,因为您的 PATH 文件夹之一上有一个 pwd 文件(什么允许您在 cmd 上写入 pwd,Windows 本身不支持 pwd 命令)
您可以进一步阅读here

关于node.js - Node.js 中的 execFile 不执行 Windows 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491399/

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