21、nodejs异步读写文件
https://blog.csdn.net/songmaolin_csdn/article/details/52910771
20、nodejs垃圾回收
node --max_semi_space_size=1 --max_old_space_size=10 1.js --production
max_semi_space_size
32 16
max_old_space_size
1.4G 0.7g
node --v8-options | grep max
dong@ubuntu:~$ node --v8-options | grep max
--gvn_iterations (maximum number of GVN fix-point iterations)
--max_inlining_levels (maximum number of inlining levels)
--max_inlined_source_size (maximum source size in bytes considered for a single inlining)
--max_inlined_nodes (maximum number of AST nodes considered for a single inlining)
--max_inlined_nodes_cumulative (maximum cumulative number of AST nodes considered for inlining)
--escape_analysis_iterations (maximum number of escape analysis fix-point iterations)
--typed_array_max_size_in_heap (threshold for in-heap typed array)
--generic_ic_threshold (max percentage of megamorphic/generic ICs to allow optimization)
--max_opt_count (maximum number of optimization attempts before giving up.)
--max_stack_trace_source_length (maximum length of function source code printed in a stack trace.)
--max_semi_space_size (max size of a semi-space (in MBytes), the new space consists of twosemi-spaces)
--max_old_space_size (max size of the old space (in Mbytes))
--max_executable_size (max size of executable memory (in Mbytes))
--print_max_heap_committed (print statistics of the maximum memory committed for the heap in name=value format on exit)
--max_object_groups_marking_rounds (at most try this many times to over approximate the weak closure)
https://yq.aliyun.com/articles/4050
19、nodejs字节流转换
buf = bytes('hello,world')
print(binascii.b2a_hex(buf))
func.bluetooth_proxy_cb(binascii.b2a_hex(buf))
/*
var s1 = Buffer.from(login.ip_info[0].ip)
var s2 = Buffer.from(login.ip_info[0].port)
var s3 = s1.toString()
var s4 = s2.toString()
var s5 = s3.substring(0,s3.length-43)
var s6 = s4.substring(0,s4.length-3)
console.log('login.ip_info.ip: ' + s5)
console.log('login.ip_info.port: ' + s6)
*/
//console.log(encodeURI(qbox10_add));
//client.connect(encodeURI(qbox10_add), '');
18、substring与substr与slice区别
var a = { 'k1':'v1', 'k2':'v2', 'k3':'v3 0123456789'};
//Object.keys(a).length
var b = a.k3
var b1 = Object.keys(a.k3).length
//var c = b.substr(0, 5);
//var c = b.slice(0,5);
var c = b.substring(0,5);
console.log(b);
console.log(c);
17、buffer
http://nodejs.cn/api/buffer.html
/*
在Node 6.0以前,直接使用new Buffer,但是这种方式存在两个问题:
参数复杂: 内存分配,还是内存分配+内容写入,需要根据参数来确定
安全隐患: 分配到的内存可能还存储着旧数据,这样就存在安全隐患
*/
// 本来只想申请一块内存,但是里面却存在旧数据
const buf9 = new Buffer(10) // <Buffer 90 09 70 6b bf 7f 00 00 50 3a>
// 不小心,旧数据就被读取出来了
buf9.toString() // '�\tpk� \u0000\u0000P:'
// 本来只想申请一块内存,但是里面却存在旧数据
const buf8 = new Buffer(10) // <Buffer 90 09 70 6b bf 7f 00 00 50 3a>
// 不小心,旧数据就被读取出来了
buf8.toString() // '�\tpk� \u0000\u0000P:'
/*
为了解决上述问题,Buffer提供了Buffer.from、Buffer.alloc、Buffer.allocUnsafe、Buffer.allocUnsafeSlow四个方法来申请内存。
*/
//buffer的基本使用
var buf1 = new Buffer('this is a tést');
// 输出: this is a tést
console.log(buf1.toString());
// 输出: this is a tC)st
console.log(buf1.toString('ascii'));
const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex');
// 输出: this is a tést
console.log(buf2.toString());
//Buffer.alloc
var test = 'hello,world'
var buf1 = Buffer.alloc(32);
buf1 = test;
console.log(buf1);
//Buffer.from
var buf2 = new ArrayBuffer(32);
//Buffer.from(arrayBuffer[, byteOffset[, length]])
var buf3 = Buffer.from(buf2, 0, 32);
buf2 = test;
console.log(buf2.length);
console.log(buf2);
buf3 = test;
console.log(buf3);
16、nodejs写文件的几种方式,哪种更好?
var fs = require('fs');
var railwayFence_file_path= './data/railwayFence.json';
//文件流方法
//var fWrite = fs.createWriteStream(railwayFence_file_path);
//fWrite.write(JSON.stringify(obj) + os.EOL);
//异步方法
fs.writeFile(railwayFence_file_path, JSON.stringify(obj),function(err){
if(err) console.log('写文件操作失败');
else console.log('写文件操作成功');
});
//同步方法
//fs.writeFileSync(railwayFence_file_path,JSON.stringify(obj));
【Node.js】'readline' 逐行读取、写入文件内容
https://blog.csdn.net/sodino/article/details/51275638
15、操作结构化数据
https://cnodejs.org/topic/56499568d28aa64101600fdc
14、nodejs共享内存
如果要共享内存,可以试试node-shm模块;如果对速度的要求没那么快,可以试试node-easy-ipc。两个模块的区别就是shared memory vs. unix domain socket。
详细的讨论(墙外链接):如何在node.js中共享内存
13、[NodeJS] Cross compile with node modules to ARM
http://xjchilli.blog.163.com/blog/static/45347739201521615514616/
12、node-gyp编译
https://github.com/nodejs/node-gyp
1) ubuntu
node-gyp configure
node-gyp build
node-gyp rebuild
2) arm cross
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export LD=arm-linux-gnueabihf-ld
export RAINLIB=arm-linux-gnueabihf-rainlib
export AR=arm-linux-gnueabihf-ar
export LINK=arm-linux-gnueabihf-g++
node-gyp configure --arch=arm
node-gyp build --arch=arm
node-gyp rebuild--arch=arm
3) nodejs库安装,编译
npm rebuild
npm init
npm install -save websocket
npm install -saveref-array
npm install -save ref-array
npm install -save ref-struct
交叉编译
npm init
npm install -save websocket --arch=arm
npm install ref-array --arch=arm
npm rebuild
4)nodejs源码交叉编译
node-ffi交叉编译
cd node-ffi
node-gyp --arch arm configure build
5)整理的交叉编译环境变量设置脚本
#!/bin/bash
export HOST=arm-linux-gnueabihf
export CPP="${HOST}-gcc -E" export STRIP="${HOST}-strip" export OBJCOPY="${HOST}-objcopy" export AR="${HOST}-ar" export RANLIB="${HOST}-ranlib" export LD="${HOST}-ld" export OBJDUMP="${HOST}-objdump" export CC="${HOST}-gcc" export CXX="${HOST}-g++" export NM="${HOST}-nm" export AS="${HOST}-as"
11、nodejs与c语言混合编程
node-c-jit, node.JS run C language Just In Time
https://github.com/zy445566/node-c-jit#example
10、nodejs调用c语言动态链接库
https://github.com/node-ffi/node-ffi
https://blog.csdn.net/u010049696/article/details/79427414
9、旧版本nodejs与c/c++之间的交互
https://cnodejs.org/topic/55215111c4f5240812f5543c
https://iojs.org/api/addons.html
https://github.com/joyent/v8plus
https://github.com/nodejs/node-gyp
8、nodejs与c/c++之间的交互
1) node-addon-examples
https://github.com/nodejs/node-addon-examples
sudo apt install nodejs-legacy
sudo apt install npm
sudo apt install node-gyp
npm install -d
node-gyp configure build
2)abi-stable-node-addon-examples
https://github.com/nodejs/abi-stable-node-addon-examples
3)node-cpp-modules
https://github.com/kkaefer/node-cpp-modules
7、判断json格式中是否含有key
http://www.cnblogs.com/zmc-change/p/5614488.html
6、在javascritp中,有两个关于定时器的专用函数
function f(){
}
1.倒计定时器:timename=setTimeout("f();",delaytime);
2.循环定时器:timename=setInterval("f();",delaytime);
5、 nodejs 关于 面向对象 及 class 的使用
https://blog.csdn.net/a120908069/article/details/54579070
4、node.js的global variable,和module.exports
https://blog.csdn.net/kyfxbl/article/details/12587385
3、nodejs module
1)将上面3des算法改造一下,做成一个module
des3.js
var crypto = require('crypto');
exports.des3Encrypt = function(param) {
var key = new Buffer(param.key); var iv = new Buffer(param.iv ? param.iv : 0) var plaintext = param.plaintext var alg = param.alg var autoPad = param.autoPad var cipher = crypto.createCipheriv(alg, key, iv); cipher.setAutoPadding(autoPad) var ciph = cipher.update(plaintext, 'utf8', 'base64'); ciph += cipher.final('base64'); return ciph; }; exports.des3Decrypt = function(param) { var key = new Buffer(param.key); var iv = new Buffer(param.iv ? param.iv : 0) var plaintext = param.plaintext var alg = param.alg var autoPad = param.autoPad var decipher = crypto.createDecipheriv(alg, key, iv); decipher.setAutoPadding(autoPad) var txt = decipher.update(plaintext, 'base64', 'utf8'); txt += decipher.final('utf8'); return txt; };
test.js
var des3 = require('./des3.js');
var data = '{"protocolHead":"abc","protocolType":0001,"userName":"user","passWord":"123","mDeviceNumber":"65535"}';
var key_qbox10 = '012345678901234567890123'; var para = { alg:'des-ede3', autoPad:true, plaintext:data, iv:null, key:key_qbox10 }; var str = des3.des3Encrypt(para); console.log(str);
运行node test.js就可以测试
2)在node中一个模块访问另一个模块中的变量和访问模块中的函数是一样的方式。
参考https://blog.csdn.net/v2810769/article/details/62429303
var a = 100;
function People(){
console.log('Hello'); } module.exports.a = a; module.exports.People = People; //只要一直添加属性就好了。这是比较推荐的。 //通过上面的例子进行导出。 var v5 = require('./5'); var People = v5.People; People(); console.log(v5.a); //以上是使用列子。
2、des3加密
在线工具 http://tool.chacuo.net/crypt3des
感谢这位哥,轻轻一百度就有现成的
http://mygo.iteye.com/blog/2018882
var assert = require('assert');
var crypto = require('crypto');
function test_des(param) { var key = new Buffer(param.key); var iv = new Buffer(param.iv ? param.iv : 0) var plaintext = param.plaintext var alg = param.alg var autoPad = param.autoPad //encrypt var cipher = crypto.createCipheriv(alg, key, iv); cipher.setAutoPadding(autoPad) //default true var ciph = cipher.update(plaintext, 'utf8', 'hex'); ciph += cipher.final('hex'); console.log(alg, ciph) //decrypt var decipher = crypto.createDecipheriv(alg, key, iv); decipher.setAutoPadding(autoPad) var txt = decipher.update(ciph, 'hex', 'utf8'); txt += decipher.final('utf8'); assert.equal(txt, plaintext, 'fail'); } test_des({ alg: 'des-ecb', autoPad: true, key: '01234567', plaintext: '1234567812345678', iv: null }) test_des({ alg: 'des-cbc', autoPad: true, key: '01234567', plaintext: '1234567812345678', iv: '12345678' }) test_des({ alg: 'des-ede3', //3des-ecb autoPad: true, key: '0123456789abcd0123456789', plaintext: '1234567812345678', iv: null }) test_des({ alg: 'des-ede3-cbc', //3des-cbc autoPad: true, key: '0123456789abcd0123456789', plaintext: '1234567812345678', iv: '12345678' })
我只用到了3des-ecb,数据格式是base64, 将加密和解密两个函数的数据格式从hex改成base64就OK了。
1、nodejs对json文件(数据)的基本操作
login.json
{
"protocolHead": "abc",
"protocolType": 0001,
"userName": "user",
"passWord": "123", "mDeviceNumber": "123" }
json_test.js
//load json file
var fs = require("fs");
var contents = fs.readFileSync("login.json"); //disserialze var obj = JSON.parse(contents); console.log(obj); //add obj.key = 'key'; //serialze var str = JSON.stringify(obj); console.log(str); //write fs.writeFile('./login_str.json', str); //disserialze var obj1 = JSON.parse(str); console.log(obj1);
运行
dong@ubuntu:~$ node json_test2.js
{ protocolHead: 'abc',
protocolType: 0001,
userName: 'user',
passWord: '123',
mDeviceNumber: '123' }
{"protocolHead":"abc","protocolType":0001,"userName":"user","passWord":"123","mDeviceNumber":"123","key":"key"}
{ protocolHead: 'abc',
protocolType: 0001,
userName: 'user',
passWord: '123',
mDeviceNumber: '123',
key: 'key' }
dong@ubuntu:~$
或者直接处理json数据
var tick = {
"protocolHead": "gis_fl",
"protocolType": 1000
}
client.send(des3_encode(JSON.stringify(tick)));
来源:oschina
链接:https://my.oschina.net/u/4397864/blog/4046253