用nodejs做一个svn密码修改页面

时间:2021-03-21 15:07:17

  linux上配置好svn服务后,管理修改密码还得去手工修改passwd这个文件,略麻烦,其实网上应该有配套的web管理修改界面程序。但我想自己用nodejs写一个,因为用node不用配置复杂的服务器。

  弄这种基于事件模型的语言,一定要小心,整个过程可能不是线性的。

 var http = require("http");
var fs = require('fs');
var url = require('url');
var query = require('querystring'); var PASSWD_FILE = "/home/svn/conf/passwd"; http.createServer(function(request, reponse) {
var params = url.parse(request.url);
var obj = query.parse(params.query);
var tm = new Date();
try {
console.log(tm.toLocaleString());
console.log(obj);
reponse.writeHead(200, {"Content-Type": "text/html"});
if(params.pathname == '/change') {
changePassword(obj.name.trim(), obj.password.trim(), obj.npassword.trim(), reponse); } else {
fs.readFile("./main.html", "utf-8", function(err, data) {
reponse.write(data);
reponse.end();
});
}
} catch(err) {
console.log(err);
}
}).listen(8000); function changePassword(name, password, npassword, reponse) {
var change = false;
fs.readFile(PASSWD_FILE, "utf-8", function(error, data) {
arr = data.split('\n');
for (var i = 0 ;i < arr.length; ++i) {
if(arr[i].indexOf("#") == -1 && arr[i].indexOf("[users]") == -1) {
ap=arr[i].split("=");
if(ap.length != 2) {
continue;
}
var account = ap[0].trim();
var passwd = ap[1].trim()
if(name == account && password == passwd && npassword.length >= 3) {
change = true;
arr[i] = "";
console.log("change account "+account + " password " + npassword);
arr[i] = account + "=" + npassword;
}
}
} var outdata = "";
for (var i = 0; i < arr.length; i++) {
if(arr[i].replace(/(^s*)|(s*$)/g, "").length ==0) {
continue;
}
outdata += (arr[i] + '\n');
}; fs.writeFile(PASSWD_FILE, outdata ,"utf-8", function(error) {
if(error || change == false) {
fs.readFile("./error.html", "utf-8", function(err, data) {
reponse.write(data);
reponse.end();
});
} else {
fs.readFile("./success.html", "utf-8", function(err, data) {
reponse.write(data);
reponse.end();
});
}
});
});
}

用nodejs做一个svn密码修改页面

启动脚本命令:nohup node main.js > node.log &

nohup & 启动程序并后台执行,不会使程序跟着控制台的死掉而死掉;> node.log把控制台的日志重定向到node.log中去。