从客户端请求运行带有节点的Bash脚本

当用户从浏览器中点击一个button时,我需要运行服务器端脚本。

我一直在研究一段时间,不能弄明白。

我们有什么:

  • 运行在Fedora Red Hat上的Node.js服务器(在本地主机上)
  • 没有PHP
  • 大多数网页是HTML + JavaScript + jQuery

更清楚的是,这里是我们想要发生的事情:

– >用户转到http:// localhost /index.html

– >用户select颜色,按下“提交”button。

– >所选颜色转到bash脚本(在服务器上)./sendColors [listOfColors]

– > bash脚本是这样的。

================

我试过的东西

child_process.spawn

我希望我可以在html页面上做到这一点:

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页面(我相信)。 当我尝试运行这个错误时,我得到的是要求是未定义的。

browserify

我试过installinst浏览器,但是我们使用的机器没有连接到互联网,不能使用npm install。 我已经手动将文件复制到usr / lib和“需要”它很好,但它说,它无法find需要“通过”,这是在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,但不知道如何使用它在我的情况

EVAL

我也试过eval也…但我认为这是阿尔布里克expression式…没有工作。

的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) 

任何人有任何想法?

这是一个简单的服务器样板(使用Express ,所以你可能需要先安装: npm install express ):

 var spawn = require('child_process').spawn; var express = require('express'); var app = express(); app.use(express.static(__dirname)); app.get('/colorsRequest', function(req, res) { var command = spawn(__dirname + '/run.sh', [ req.query.color || '' ]); var output = []; command.stdout.on('data', function(chunk) { output.push(chunk); }); command.on('close', function(code) { if (code === 0) res.send(Buffer.concat(output)); else res.send(500); // when the script fails, generate a Server Error HTTP response }); }); app.listen(3000); 

你可以给它传递一个颜色,并且它将会使用作为parameter passing的颜色运行shellscript run.sh (它假定它与服务器JS文件位于同一个目录中):

 curl -i localhost:3000/colorsRequest?color=green # this runs './run.sh green' on the server 

这是一个样板HTML页面(将其保存为index.html ,将其放在与服务器代码和shell脚本相同的目录中,启动服务器,然后在浏览器中打开http://localhost:3000 ):

 <!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> 

你的第一个方法是child_process.spawn变种, 当然,你不能把它放在HTML页面中,因为它是在浏览器中执行,而不是在服务器上执行,但是你可以很容易地在浏览器中创build一个请求(根据你需要的AJAX或页面加载)触发器在服务器中运行这个脚本。