有没有办法从AngularJS控制器发出CLI命令? [重复]

问题描述 投票:0回答:1

我创建了一项服务,可以使用child-process从我的AngularJS控制器中进行一些CLI /终端命令,但似乎无法执行该操作。以下是我的服务:

listFiles.js:

'use strict'
var exec = require('child_process').exec;


const listFiles = function(){
    exec('ls', (err, stdout, stderr) => {
      if (err) {
        // node couldn't execute the command
        return;
      }

      // the *entire* stdout and stderr (buffered)
      console.log('stdout: ' + stdout);
      console.log(stderr);
    });
}

export default listFiles;

我还向我的package.json中添加了以下内容:

"browser": { 
    "fs": false, 
    "child_process": false 
  },

我收到以下错误:

TypeError:exec不是函数

我正在使用webpack来构建应用程序。有没有办法在AngularJS控制器中/或作为AngularJS服务进行CLI命令?

javascript node.js angularjs webpack cmd
1个回答
1
投票

请看以下问题:

Angularjs - Require('child_process')

总结:您不能通过浏览器应用程序运行子进程,因为这将是一个巨大的安全问题。

如果要在服务器上运行某些命令,请使用NodeJS或任何其他服务器技术编写REST API,然后从AngularJS应用程序中调用它。

更新:

这是一个如何使用express处理HTTP GET请求的简单示例。

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    const command = req.query. command // Get the command form the query parameters.
    // DO WHATEVER YOU WANT TO DO USING child_process

});

app.listen(3000, function () {
    console.log('App listening on port 3000');
});
© www.soinside.com 2019 - 2024. All rights reserved.