标签:
linux上配置好svn后,管理修改密码还得去手工修改passwd这个文件,太麻烦,其实网上应该有配套的web管理修改界面。但我想自己用nodejs写一个,因为用node不用配置复杂的服务器。
弄这种基于事件模型的语言,一定要小心,整个过程可能不是线性的。
1 var http = require("http"); 2 var fs = require(‘fs‘); 3 var url = require(‘url‘); 4 var query = require(‘querystring‘); 5 6 var PASSWD_FILE = "/home/svn/conf/passwd"; 7 8 http.createServer(function(request, reponse) { 9 var params = url.parse(request.url); 10 var obj = query.parse(params.query); 11 var tm = new Date(); 12 try { 13 console.log(tm.toLocaleString()); 14 console.log(obj); 15 reponse.writeHead(200, {"Content-Type": "text/html"}); 16 if(params.pathname == ‘/change‘) { 17 changePassword(obj.name.trim(), obj.password.trim(), obj.npassword.trim(), reponse); 18 19 } else { 20 fs.readFile("./main.html", "utf-8", function(err, data) { 21 reponse.write(data); 22 reponse.end(); 23 }); 24 } 25 } catch(err) { 26 console.log(err); 27 } 28 }).listen(8000); 29 30 function changePassword(name, password, npassword, reponse) { 31 var change = false; 32 fs.readFile(PASSWD_FILE, "utf-8", function(error, data) { 33 arr = data.split(‘\n‘); 34 for (var i = 0 ;i < arr.length; ++i) { 35 if(arr[i].indexOf("#") == -1 && arr[i].indexOf("[users]") == -1) { 36 ap=arr[i].split("="); 37 if(ap.length != 2) { 38 continue; 39 } 40 var account = ap[0].trim(); 41 var passwd = ap[1].trim() 42 if(name == account && password == passwd && npassword.length >= 3) { 43 change = true; 44 arr[i] = ""; 45 console.log("change account "+account + " password " + npassword); 46 arr[i] = account + "=" + npassword; 47 } 48 } 49 } 50 51 var outdata = ""; 52 for (var i = 0; i < arr.length; i++) { 53 if(arr[i].replace(/(^s*)|(s*$)/g, "").length ==0) { 54 continue; 55 } 56 outdata += (arr[i] + ‘\n‘); 57 }; 58 59 fs.writeFile(PASSWD_FILE, outdata ,"utf-8", function(error) { 60 if(error || change == false) { 61 fs.readFile("./error.html", "utf-8", function(err, data) { 62 reponse.write(data); 63 reponse.end(); 64 }); 65 } else { 66 fs.readFile("./success.html", "utf-8", function(err, data) { 67 reponse.write(data); 68 reponse.end(); 69 }); 70 } 71 }); 72 }); 73 }
启动脚本命令:nohup node main.js > node.log &
nohup & 启动程序并后台执行,不会使程序跟着控制台的死掉而死掉;> node.log把控制台的日志重定向到node.log中去。
标签:
原文地址:http://www.cnblogs.com/wendao/p/4952503.html