10

ブラウザのユーザーがボタンをクリックしたときにサーバー側のスクリプトを実行する必要があります...

ずっと調べているのですが、わかりません。

私たちが持っているもの:

  • Fedora Red Hat で実行されている Node.js サーバー (localhost 上)
  • いいえ 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 にコピーし、それを正常に「required」しましたが、browserify の index.js にある「介して」require が見つからないと言われました...

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 を試しました...しかし、それは代数式のためだと思います...うまくいきませんでした。

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);
    });

colorRequest.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)

誰でもアイデアはありますか?

4

2 に答える 2

19

これはサーバーの単純なボイラープレートです (これは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);

色を渡すと、引数として渡された色を使用してシェルスクリプトrun.sh(サーバーの JS ファイルと同じディレクトリにあると想定) が実行されます。

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

ボイラープレート HTML ページを次に示します (名前をindex.html付けて保存し、サーバー コードおよびシェル スクリプトと同じディレクトリに配置し、サーバーを起動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>
于 2013-05-16T18:58:04.347 に答える
4

あなたは最初のアプローチであるchild_process.spawnバリアントで正しい道を進んでいます。もちろん、これを HTML ページに配置することはできません。これは、サーバーではなくブラウザーで実行されるためです。ただし、ブラウザーで要求を簡単に作成できます (必要に応じて、AJAX またはページの読み込み)。サーバーでこのスクリプトを実行するトリガー。

于 2013-05-16T16:35:51.440 に答える