node中一堆凌乱的小知识

xml相关

xml转json: npm包 xml2js

1
2
3
4
5
6
7
//explicitArray:false 返回json的value不为数组,explicitRoot:false,去掉头部
var xml='<xml><a>1</a><b>2</b></xml>';
blue.promisify(new xml2js.Parser({explicitArray:false,explicitRoot:false}).parseString)(xml).then(function(result){
console.dir(result);
},function(error){
//{ a: '1', b: '2' }
})

json转xml:npm包 xmlbuilder

1
2
3
4
5
6
7
8
9
10
11
//‘xml’ root名称,headless: true 去掉 xml 头部,pretty: true 格式化
var json = {
a: 1,
b: 2
};
console.error(xmlbuilder.create('xml',{headless: true}).ele(json).end({ pretty: true}))
<xml>
<a>1</a>
<b>2</b>
</xml>

bluebird

promise.promisify 将api promise 化

prcess

To set an environment variable in Windows:

1
SET NODE_ENV=development

on OS X or Linux

1
export NODE_ENV=development