【问题标题】:Run Bash Script with Node from client request从客户端请求使用节点运行 Bash 脚本
【发布时间】:2013-05-11 15:59:08
【问题描述】:

当浏览器中的用户单击按钮时,我需要运行服务器端脚本...

我研究了一段时间,还是搞不明白。

我们有什么:

  • Node.js 服务器(在本地主机上)在 Fedora Red Hat 上运行
  • 没有 PHP
  • 大部分页面都是html + javascript + jQuery

更清楚地说,这是我们希望发生的事情:

-->用户访问 http://localhost/index.html

-->用户选择颜色,按下“提交”按钮。

-->选定的颜色进入 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 页面上运行它(我相信)。当我尝试运行它时遇到的错误是 require 未定义。

浏览

我试过 installinst browserify,但是我们使用的机器没有连接到互联网,并且无法使用 npm install。我已经手动将文件复制到 usr/lib 并“需要”它,但它说它找不到需要“通过”,它在 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;
        }

说它不知道 Runtime 是什么

RequireJS

我研究了 RequireJS,但不明白如何在我的情况下使用它

评估

我也试过 eval...但我认为那是代数表达式...没有用。

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)

有人有什么想法吗?

【问题讨论】:

  • 您将无法从浏览器运行脚本(如果可能,请考虑安全隐患),因此您必须将事件中继到您的服务器并运行脚本在那里。
  • 你为什么要在客户端运行它?让您的客户端脚本向 Node 服务器发送请求,然后执行此服务器端。返回您的结果。
  • 哦,好的!我试试看

标签: javascript jquery node.js


【解决方案1】:

这是一个简单的服务器样板(使用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);

你可以给它一个颜色,它会运行 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>

【讨论】:

  • 我如何让它在带有 cygwin 的 Windows 上运行?
  • @gaitat 我不知道,对不起:(
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多