当浏览器中的用户单击按钮时,我需要运行服务器端脚本...
我研究了一段时间,没搞明白。
我们有什么:
- 在 Fedora Red Hat 上运行的 Node.js 服务器(在本地主机上)
- NO 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 并“require”它很好,但后来它说找不到 require“through”,这是在 browserify 的 index.js 中...
获取运行时
尝试过这个东西:
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 是什么
要求JS
我研究过 RequireJS,但不明白如何在我的案例中使用它
eval
我也尝试过 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);
});
颜色请求.js
var RequestClass = function() {
console.log("HELLO");
};
// now expose with module.exports:
exports.Request = RequestClass;
索引.html
...
var colorsList = ...
...
$.get('http://localhost/colorsRequest', function(colors) {
$('#response').html(colorsList); // show the list
});
我越来越
GET http://localhost/colorsRequest 404 (Not Found)
有人有什么想法吗?