2013-05-16 52 views
9

我需要的時候從瀏覽器用戶點擊一個按鈕,一個服務器端腳本運行...運行bash腳本與來自客戶端的請求節點

我已經研究了一段時間,而不能想辦法。

我們有什麼:

  • 的Node.js服務器(在本地主機上)在Fedora紅帽運行
  • 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頁面上運行它(我相信)。當我嘗試運行這個錯誤時,我得到的錯誤是需求未定義。

browserify

我試過installinst browserify,但我們正在使用的機器沒有連接到互聯網,而不能使用NPM安裝。我已經手動將文件複製到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; 
     } 

說,不知道什麼是運行系統

RequireJS

我看着RequireJS,但不知道如何在我的情況下使用它

EVAL

我試過的eval也...但我認爲這是對代數表達式...沒有工作。

的ActiveX

甚至嘗試的ActiveX:

variable=new ActiveXObject(... 

表示,不知道是什麼的ActiveXObject是

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

目前我在想什麼

HttpSer ver.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) 

任何人有什麼想法?

+1

您將無法從瀏覽器中運行的腳本(如果可能的話,考慮安全影響),所以你將不得不將事件轉發到你的服務器並在那裏運行腳本。 – robertklep

+1

你爲什麼試圖在客戶端運行它?讓您的客戶端腳本向節點服務器發送請求,然後執行此服務器端。返回你的結果。 –

+0

哦,好的!我正在試試 – Kayvar

回答

17

下面是對服務器的簡單樣板(使用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); 

你可以傳遞一種顏色,它將運行shell腳本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> 
+0

真棒,感謝您的詳細答案!我會試試這個! – Kayvar

+0

我怎麼會得到這個與cygwin在Windows上運行? – gaitat

+0

@gaitat我不知道,對不起 – robertklep

3

您的第一個方法child_process.spawn變體正確。當然,你不能把它放在HTML頁面中,因爲它是在瀏覽器中執行的,而不是在服務器中執行的,但是你可以很容易地在瀏覽器中創建一個請求(根據你需要的AJAX或頁面加載),觸發器在服務器中運行此腳本。

+0

謝謝!我想弄清楚現在怎麼做 – Kayvar

+0

這似乎是它的一部分:http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp – Kayvar