Javascript 从客户端请求使用节点运行Bash脚本
当浏览器中的用户单击按钮时,我需要运行服务器端脚本 我已经研究了一段时间了,但还是搞不懂 我们所拥有的:Javascript 从客户端请求使用节点运行Bash脚本,javascript,jquery,node.js,Javascript,Jquery,Node.js,当浏览器中的用户单击按钮时,我需要运行服务器端脚本 我已经研究了一段时间了,但还是搞不懂 我们所拥有的: Node.js服务器(在本地主机上)在Fedora Red Hat上运行 没有PHP 大多数页面是html+javascript+jQuery 更清楚地说,以下是我们希望发生的事情: -->用户转到http://localhost/index.html -->用户选择颜色,按下“提交”按钮。 -->选定的颜色将转到bash脚本(在服务器上)。/sendColors[listOfColor
- Node.js服务器(在本地主机上)在Fedora Red Hat上运行
- 没有PHP
- 大多数页面是html+javascript+jQuery
var spawn = require('child_process').spawn;
ls = spawn(commandLine, [listOfColors]);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('close', function (code) {
console.log('child process exited with code ' + code);
});
但是这个脚本是服务器端的,不是客户端的,所以我不能在html页面上运行它(我相信)。当我尝试运行此命令时,出现的错误是require未定义
browserify
我已尝试installinst browserify,但我们正在使用的计算机未连接到internet,无法使用npm install。我已经手动将这些文件复制到usr/lib,并且“required”很好,但是它说它找不到require“through”,它位于browserify的index.js中
getRuntime
试过这个东西:
var bash_exit_code = 0; // global to provide exit code from bash shell invocation
function bash(command)
{
var c; // a character of the shell's stdout stream
var retval = ""; // the return value is the stdout of the shell
var rt = Runtime.getRuntime(); // get current runTime object
var shell = rt.exec("bash -c '" + command + "'"); // start the shell
var shellIn = shell.getInputStream(); // this captures the output from the command
while ((c = shellIn.read()) != -1) // loop to capture shell's stdout
{
retval += String.fromCharCode(c); // one character at a time
}
bash_exit_code = shell.waitFor(); // wait for the shell to finish and get the return code
shellIn.close(); // close the shell's output stream
return retval;
}
说它不知道什么是运行时
RequireJS
我研究过RequireJS,但不知道如何在我的案例中使用它
评估
我也试过评估。。。但我认为这是针对代数表达式的。。。没用
ActiveX
甚至尝试过activeX:
variable=new ActiveXObject(...
说它不知道ActiveXObject是什么
================
目前我正在尝试的
HttpServer.js:
var http = require('http');
...
var colors = require('./colorsRequest.js').Request;
...
http.get('http://localhost/colorsRequest', function(req, res){
// run your request.js script
// when index.html makes the ajax call to www.yoursite.com/request, this runs
// you can also require your request.js as a module (above) and call on that:
res.send(colors.getList()); // try res.json() if getList() returns an object or array
console.log("Got response ");// + res.statusCode);
});
colorsRequest.js
var RequestClass = function() {
console.log("HELLO");
};
// now expose with module.exports:
exports.Request = RequestClass;
index.html
...
var colorsList = ...
...
$.get('http://localhost/colorsRequest', function(colors) {
$('#response').html(colorsList); // show the list
});
我要走了
GET http://localhost/colorsRequest 404 (Not Found)
有人有什么想法吗?您的第一个方法是
child\u过程。spawn
variant。当然,您不能将其放在HTML页面中,因为它随后在浏览器中执行,而不是在服务器中执行,但您可以在浏览器中轻松创建一个请求(AJAX或页面加载,取决于您的需要),从而触发在服务器中运行此脚本。这里是一个简单的服务器模板(它使用,因此您可能需要先安装:npm install express
):
您可以向它传递一种颜色,它将运行shellscriptrun.sh
(假定它与服务器JS文件位于同一目录中),并将颜色作为参数传递:
curl -i localhost:3000/colorsRequest?color=green
# this runs './run.sh green' on the server
这是一个样板HTML页面(另存为index.HTML
,将其与服务器代码和shell脚本放在同一目录中,启动服务器,然后打开http://localhost:3000
在浏览器中):
绿色
蓝色
黄色的
橙色
$('select')。在('change',function()上{
$.get('/colorsRequest',{color:$(this.val()});
});
您将无法从浏览器运行脚本(如果可能,请考虑安全问题),因此您必须将事件中继到服务器并在那里运行脚本。为什么要在客户端运行此脚本?让客户端脚本向节点服务器发送请求,然后在服务器端执行此操作。返回结果。哦,好吧!我正在尝试运行此脚本!我正在尝试现在如何执行此操作这似乎是it:我怎样才能让它在有cygwin的windows上运行?@gaitat我不知道,对不起:(
curl -i localhost:3000/colorsRequest?color=green
# this runs './run.sh green' on the server
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<select>
<optgroup label="Pick a color:">
<option>green</option>
<option>blue</option>
<option>yellow</option>
<option>orange</option>
</optgroup>
</select>
<script>
$('select').on('change', function() {
$.get('/colorsRequest', { color : $(this).val() });
});
</script>
</body>
</html>