将variables从Jquery传递给Node来运行git命令

我有一个下拉列表,它包含了html文件中分支的名字。 我想把这个名字插入到下面的节点代码中,以便每当用户点击一个下拉的值时,它应该把它传递给节点并运行命令“git log BRANCHNAME”并存储它Json(现在):

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格式,如果我也能得到帮助,我也需要用右括号分割每个对象。

您可以在<select>标签中捕获change事件,并将选定的分支发送到服务器。 然后在服务器上创build一个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){ ... }) 

是当我们处理客户端请求时。 请注意使用middleware body-parser来获取body属性中的variables。

客户端代码( 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> 

这里我们在select<option>后将请求发送到服务器:

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

branches.json位于public文件夹中。 用这一行: app.use(express.static(path.join(__dirname, 'public'))); 我们使公用文件夹内的所有内容都可用于客户端。 这是branches.jason文件的外观:

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