2016-02-29 99 views
1

我有一個下拉列表,其中包含html文件中分支的名稱。 我想取這個名字並將它插入到下面的節點代碼中,這樣每當用戶單擊下拉列表中的某個值時,它應該將它傳遞給節點並運行命令「git log BRANCHNAME」並存儲它Json(現在) :將變量從Jquery傳遞給Node以運行git命令

var sys = require('sys') 
var express = require('express'); 
var app = express(); 


app.get('handle', function(request,response,error){ 
var branchName = request.body.branchname; 
console.log("Branch name"+branchName+""); 
if(error){ 
console.log("error"); 
} 
var exec = require('child_process').exec; 
var fs = require('fs'); 

    function put(error, stdout, stderr) { 
    var commitsbybranch = JSON.stringify(stdout.split(/\r?\n/).map(function(e) { return e.substring(0);}).filter(function(e) { return e; })); 
    fs.writeFile('reacted/testcommitsbybranch.json', commitsbybranch); 
    } 
    exec("git log "+branchName+"", put); 
    console.log("Pulling commits by branch done"); 
    } ) 


    app.listen(3000); 

我jQuery代碼是這樣的,所以當我點擊我想要的值傳遞到上面的節點代碼的選項

 $.getJSON("Branches.json", function (data) { 
        $.each(data, function (index, item) { 
         $('#users-dropdown').append(
          $('<option></option>').val(item).html(item) 
     ); 
    }); 

}); 

我還需要有JSON的正確格式,如果我能我也需要幫助 我需要用右括號分割每個對象。

+0

您打算如何從網頁獲取數據到節點代碼?你有某種REST服務嗎? –

+0

不,我不知道。我只有html頁面和jQuery代碼,然後是節點。但沒有一個連接在一起 – Gill

+0

如果不設置某種服務器,則無法將HTML代碼連接到Node上 - 它們完全分開運行。 –

回答

1

您可以在<select>標記中捕獲change事件並將所選分支發送到服務器。然後在服務器上創建一個route以捕捉客戶端發出的請求(使用branchname)並執行您的邏輯。這是一個例子:

服務器:

var express = require('express'); 
var path = require('path'); 
var bodyParser = require('body-parser'); 
var fs = require('fs'); 

var app = express(); 


app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.get('/', function(req, res){ 
    res.sendFile(path.join(__dirname+'/handle.html')); 
}); 


app.post('/handle', function(request, response, next){ 
    var branchName = request.body.branchname; 
    console.log("Branch name"+branchName+""); 

    var exec = require('child_process').exec; 


    function put(error, stdout, stderr) { 
     var commitsbybranch = JSON.stringify(stdout.split(/\r?\n/).map(function(e) { return e.substring(0);}).filter(function(e) { return e; })); 
     fs.writeFile('reacted/testcommitsbybranch.json', commitsbybranch); 
    } 
    exec("git log "+branchName+"", put); 
    console.log("Pulling commits by branch done"); 
}); 


app.listen(3000); 

的代碼:

app.post('/handle', function(request, response, next){ 
... 
}) 

是,當我們處理客戶端請求。請注意使用middlewarebody-parser來獲取body屬性中的變量。

客戶端代碼(handle.html):

<!DOCTYPE html> 
<html> 
<head> 
    <title>Git Branches</title> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 

      $('#users-dropdown').change(function(){ 
       var branch = $("#users-dropdown option:selected").text(); 
       $.post('handle', {branchname: branch}, function(data){ 
        console.log(data); 
       }); 
      }); 

      $.getJSON("branches.json", function (data) { 
       $.each(data.branches, function (index, item) { 
        $('#users-dropdown').append($('<option>', {value: item.branch, text: item.branch})); 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <div> 
     <select id="users-dropdown"> 
     </select> 
    </div> 
</body> 
</html> 

在這裏,我們發送<option>後的請求到服務器選擇:

$('#users-dropdown').change(function(){ 
    var branch = $("#users-dropdown option:selected").text(); 
    $.post('handle', {branchname: branch}, function(data){ 
     console.log(data); 
    }); 
}); 

branches.jsonpublic文件夾中。使用此行:app.use(express.static(path.join(__dirname, 'public')));我們將public文件夾中的所有內容都提供給客戶端。這是branches.jason文件的外觀:

{ 
    "branches": [ 
        { 
         "branch": "branch1" 
        }, 
        { 
         "branch": "branch2" 
        }, 
        { 
         "branch": "branch3" 
        }, 
        { 
         "branch": "branch4" 
        } 
       ] 
} 
+0

謝謝,我的HTML頁面上出現錯誤,現在json不加載。但我的json文件是這樣的[「master」,「branch1」,「branch2」] – Gill

+0

有什麼錯誤? – leobelizquierdo

+0

它說branches.json沒有找到,並且它在同一個文件夾結構中 – Gill