javascript - 将变量从 Jquery 传递到 Node 以运行 git 命令

标签 javascript node.js git

我有一个下 pull 列表,其中包含 html 文件中的分支名称。 我想获取该名称并将其插入到下面的 Node 代码中,以便每次用户单击下 pull 列表中的值时,它都应该将其传递给 Node 并运行命令“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 代码是这样的,所以当我单击该选项时,我希望将值传递给上面的 Node 代码

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

});

我还需要有正确的 JSON 格式,如果我也能获得这方面的帮助,请 我需要用右大括号分割每个对象。

最佳答案

你可以捕获change事件在<select>标记并将选定的分支发送到服务器。然后在服务器中创建一个route捕获从客户端(带有分支名称)发送的请求并执行您的逻辑。这是一个例子:

服务器:

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 中的变量属性。

客户端代码 ( 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.json位于 public文件夹。有了这一行:app.use(express.static(path.join(__dirname, 'public')));我们在 public 内制作一切可供客户使用的文件夹。 branches.jason就是这样的文件看起来:

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

关于javascript - 将变量从 Jquery 传递到 Node 以运行 git 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35703183/

相关文章:

git - 如何使用 opendiff 作为默认 merge 工具

javascript - 如何建立无障碍画廊?

php 字符串到 javascript 代码用逗号除了最后一个字符串

javascript - JS_of_ocaml - 获取 cookie 时出错

node.js - 在 travis 上运行 child_process.exec 失败,而不是在本地

git - Visual Studio Online - 无法解析版本描述符 <BranchName>

JavaScript - 跳转到 anchor

javascript - 我无法在html页面上显示json文件中的数据

node.js - 为什么我们要为 Angular 2.0 安装 Node.js?

git - 如何使用 `git grep --not`