node.js模块的坑

在写一个工具的时候,需要将xml转为json方便处理,以前电脑上装的node.js的版本为0.8,结果我再安装node-xml2json时提示版本过低,然后我又重装安装了最新版本。

然后再次尝试安装,首先报python版本不对,不支持3.0,然后再降级,再然后说windows系统需要先安装node-expat,装这个模块又需要先装node-gyp,好吧然后我一直安装失败…

 

最后我使用了这个模块: node-xml2json   根据thomasfrank大师写的 XML to JSON 改造的,唯一要注意的是如果xml中的节点属性有大写,转换之后全部成小写了。

 

工具的功能从本地上传文件至FTP,然后通过外网地址(模拟游戏玩家请求CDN操作)下载一份XML配置,通过配置下载相应的zip包,校验其大小以及CRC

 

下载zip使用如下的方法:

request('http://xx.zip').pipe(fs.createWriteStream('xx.zip')).on('close', function () {
console.log('File written!');
});

获取crc的值
var val = crc.crc32(fs.readFileSync("xx.zip", null)).toString(10);
console.log(val);

 

工具写完了,使用了几个模块,觉得这几个模块都还很不错,有需要的同学可以自取:)

1、node-ftp

操作ftp,上传、下载文件。这里有一个例子,可以参考一下:

http://blog.jonathanchannon.com/2014/03/22/using-node-and-ftp-with-promises/

var path = require('path');
var fs = require('fs');
var Promise = require('bluebird');
var Client = require('ftp');

var c = new Client();

var connectionProperties = {
host: "myhost",
user: "myuser",
password: "mypwd"
};

c.on('ready', function () {
console.log('ready');
c.list(function (err, list) {
if (err) throw err;
list.forEach(function (element, index, array) {
//Ignore directories
if (element.type === 'd') {
console.log('ignoring directory ' + element.name);
return;
}
//Ignore non zips
if (path.extname(element.name) !== '.zip') {
console.log('ignoring file ' + element.name);
return;
}
//Download files
c.get(element.name, function (err, stream) {
if (err) throw err;
stream.once('close', function () {
c.end();
});
stream.pipe(fs.createWriteStream(element.name));
});
});
});
});

c.connect(connectionProperties);

2、request

3、node-crc

4、node-xml2json

posted @ 2014-09-16 16:17  meteoric_cry  阅读(11450)  评论(0编辑  收藏  举报