ブラウザのユーザーがボタンをクリックしたときにサーバー側のスクリプトを実行する必要があります...
ずっと調べているのですが、わかりません。
私たちが持っているもの:
- 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)
誰でもアイデアはありますか?