我已经创建了一个服务来使用我的AngularJS控制器使用child-process来生成一些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命令的方法?
发布于 2019-11-04 19:53:46
请看以下问题:
总之:您不能通过浏览器应用程序运行子进程,因为这将是一个巨大的安全问题。
如果您想在服务器上运行某些命令,请使用NodeJS或任何其他服务器技术编写REST,并从您的AngularJS应用程序中调用它。
更新:
下面是一个简单的示例,说明如何使用express来处理HTTP请求。
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');
});您可以在正式的ExpressJs文档中找到不同的例子。
https://stackoverflow.com/questions/58700036
复制相似问题