我有一个下 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/